通知的 PendingIntent 不要第二次调用我的 Activity

发布于 2024-12-03 07:39:15 字数 3298 浏览 0 评论 0原文

我希望接收推送通知(c2dm)接收显示Notification。此通知以 PendingIntent 启动,这是一个显示弹出窗口的活动。单击“确定”按钮时,此弹出窗口将启动我的应用程序。

这是在接收推送通知时执行的代码:

private void dealMessage(Context context, Intent intent)
{       
    try
    {
        String message = intent.getExtras().getString("message");

        Log.v("testc2dm","message :     [" + message + "]");

        //create bean of my Notification
        NotificationPush notif = new NotificationPush();
        notif.init((JSONObject)JSONValue.parse(message));

        //create PendingIntent
        Intent intentDialog = new Intent(context, DialogActivity.class);
        intentDialog.putExtra("notif", notif);
        int requestCode= (int) System.currentTimeMillis();
        PendingIntent pi = PendingIntent.getActivity(context, requestCode, intentDialog, PendingIntent.FLAG_ONE_SHOT);

        //Create the Notification
        Notification n = new Notification();
        n.flags |= Notification.FLAG_SHOW_LIGHTS; // allume l'écran
        n.flags |= Notification.FLAG_AUTO_CANCEL; // fait disparaitre automatiquemet la notif apres un clic dessus
        n.defaults = Notification.DEFAULT_ALL;
        n.icon = R.drawable.icon;
        n.when = System.currentTimeMillis();
        n.setLatestEventInfo(context, "Mon titre", notif.getTitre(), pi);

        //add my Notification
        NotificationManager notificationManager = (NotificationManager)context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(ID_NOTIFICATION, n);
    }
    catch(Exception e)
    {
        Log.e("testc2dm","error :       [" + e.getMessage() + "]");
        e.printStackTrace();
    }
}

这是我的活动,显示弹出窗口并启动我的应用程序:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Log.v("testc2dm","DialogActivity oncreate()");

    //recovery of my bean Notification
    Intent intentParam = getIntent();
    NotificationPush notif = (NotificationPush)intentParam.getSerializableExtra("notif");

    if(notif != null)
    {           
        Log.v("testc2dm","notif => titre [" + notif.getTitre() + "] -- msg [" + notif.getMessage() + "] -- type [" + notif.getType() + "]");

        //display popup
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(notif.getMessage());
        builder.setTitle(notif.getTitre());
        builder.setCancelable(false);
        builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                if(!TestC2DMActivity.appIsRunning)
                {
                    //launch first Activity of my application
                    Intent intent = new Intent(DialogActivity.this, TestC2DMActivity.class);
                    startActivity(intent);
                }
            }
        });
        builder.show();
    }
}

我的问题是:如果我的应用程序将通过接收推送来启动(c2dm>通知> PendingIntent> DialogActivity > TestC2DMActivity) 然后接收下一次推送,Notification 会正常显示,但点击该 Notification 不会启动 DialogActivity。而当应用程序正常启动时(应用程序的图标),则一切正常。

我觉得如果我的应用程序是由我的 PendingIntent 启动的,那么这个 PendingIntent 不再需要启动 DialogActivity .. 为什么?

真的感谢您的帮助,并对我的英语不好感到抱歉。

I hope that receiving a push-notification (c2dm) reception display Notification. This Notification start, whith PendingIntent, an Activity that display a pop-up. This popup launch my application when clicking on the "ok" button.

Here is the code executed at the reception of push-notification :

private void dealMessage(Context context, Intent intent)
{       
    try
    {
        String message = intent.getExtras().getString("message");

        Log.v("testc2dm","message :     [" + message + "]");

        //create bean of my Notification
        NotificationPush notif = new NotificationPush();
        notif.init((JSONObject)JSONValue.parse(message));

        //create PendingIntent
        Intent intentDialog = new Intent(context, DialogActivity.class);
        intentDialog.putExtra("notif", notif);
        int requestCode= (int) System.currentTimeMillis();
        PendingIntent pi = PendingIntent.getActivity(context, requestCode, intentDialog, PendingIntent.FLAG_ONE_SHOT);

        //Create the Notification
        Notification n = new Notification();
        n.flags |= Notification.FLAG_SHOW_LIGHTS; // allume l'écran
        n.flags |= Notification.FLAG_AUTO_CANCEL; // fait disparaitre automatiquemet la notif apres un clic dessus
        n.defaults = Notification.DEFAULT_ALL;
        n.icon = R.drawable.icon;
        n.when = System.currentTimeMillis();
        n.setLatestEventInfo(context, "Mon titre", notif.getTitre(), pi);

        //add my Notification
        NotificationManager notificationManager = (NotificationManager)context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(ID_NOTIFICATION, n);
    }
    catch(Exception e)
    {
        Log.e("testc2dm","error :       [" + e.getMessage() + "]");
        e.printStackTrace();
    }
}

this is my activity that displays popup and start my application :

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Log.v("testc2dm","DialogActivity oncreate()");

    //recovery of my bean Notification
    Intent intentParam = getIntent();
    NotificationPush notif = (NotificationPush)intentParam.getSerializableExtra("notif");

    if(notif != null)
    {           
        Log.v("testc2dm","notif => titre [" + notif.getTitre() + "] -- msg [" + notif.getMessage() + "] -- type [" + notif.getType() + "]");

        //display popup
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(notif.getMessage());
        builder.setTitle(notif.getTitre());
        builder.setCancelable(false);
        builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                if(!TestC2DMActivity.appIsRunning)
                {
                    //launch first Activity of my application
                    Intent intent = new Intent(DialogActivity.this, TestC2DMActivity.class);
                    startActivity(intent);
                }
            }
        });
        builder.show();
    }
}

My problem is : if my application will be started by receiving a push (c2dm > Notification > PendingIntent > DialogActivity > TestC2DMActivity) then receive the next push, the Notification will appear fine, but clicking on the Notification will not launch DialogActivity. Whereas when the application is started normally (icon of the app) then everything works fine.

I feel that if my application is started by my PendingIntent, then this PendingIntent no longer want launch DialogActivity .. Why??

really thanks for your help and sorry for my bad english..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2024-12-10 07:39:15

我有解决方案:

intentDialog.setAction("" + Math.random());

从pendingIntent启动的Intent获取额外

无论如何,谢谢你

I have solution :

intentDialog.setAction("" + Math.random());

getExtra from Intent launched from a pendingIntent

thank you anyway

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文