从带有数据的通知启动应用程序

发布于 2024-12-17 06:35:52 字数 1225 浏览 0 评论 0原文

我正在尝试使用 C2DM 在我的 Android 手机上触发推送通知,并让用户单击通知并在应用程序中启动特定活动。我如何传递这些信息?

目前我正在执行以下操作:

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager 
            = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification23;

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;  

        Context appContext = getApplicationContext();
        Intent notificationIntent = new Intent(this, RequestDialogActivity.class);          
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra(LocationHandler.KEY_ORIGINATOR, number);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);

        final int id = 1;

        mNotificationManager.notify(id, notification);

我尝试通过在 notificationIntent 上调用 putExtra 来存储状态,但新活动始终具有 null savingInstanceState Bundle。我如何传递信息?

I'm trying to use C2DM to trigger a push notification on my android phone, and have the user click the notification and launch a certain activity in the app. How can I pass this information along?

Currently I'm doing the following:

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager 
            = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification23;

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;  

        Context appContext = getApplicationContext();
        Intent notificationIntent = new Intent(this, RequestDialogActivity.class);          
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra(LocationHandler.KEY_ORIGINATOR, number);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);

        final int id = 1;

        mNotificationManager.notify(id, notification);

I'm trying to store state by calling putExtra on the notificationIntent, but the new activity always has a null savedInstanceState Bundle. How can I pass information along?

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

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

发布评论

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

评论(1

再浓的妆也掩不了殇 2024-12-24 06:35:52

您在意图中设置的额外内容必须通过 getIntent().getExtras() 检索 savedInsanceState 更多的是在应用程序关闭时检索状态系统并且您想要恢复用户上次看到的相同状态。

the extras you set in the intent have to be retrieved by the getIntent().getExtras() the savedInsanceState is more about retrieve the state when the application have been closed by the system and you want to recover the same state that user saw last time.

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