【问题标题】:Get message from Notification and display on the app从通知中获取消息并显示在应用程序上
【发布时间】:2013-11-24 13:37:26
【问题描述】:

我正在尝试使用 GCM 开发推送通知服务。我能够接收从服务器发送到我的设备的通知。单击通知后,应用程序将打开。好的。现在我想要的是通知上的消息也应该显示在应用程序上。我该怎么做?

Code in GCMintentservice.java:
private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_action_search_)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

现在我收到了通知,但点击通知后,我进入了应用程序,但消息不存在。我该怎么办。我在主应用程序中有一个文本视图。如何使用通知中的消息更新该文本视图?可能在主应用程序中的 OnCreate 上做些什么?请帮忙。

问候

【问题讨论】:

    标签: android android-intent notifications push-notification google-cloud-messaging


    【解决方案1】:

    您应该将包含 GCM 详细信息的 Bundle 提供到 PendingIntent 的包装 Intent 中:

    Intent intent = new Intent(this, MainActivity.class)
    intent.putExtra(...);
    PendingIntent contentIntent = PendingIntent.getActivity(
            this, 0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

    • 我应该如何获取 MainActivity 类中的值?
    • @Saty 看来您缺少一些基础知识。我建议阅读what Intents are,然后跟进this information about Notifications
    • 我尝试了所有意图的事情,但是我认为设置的两个标志(notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP))正在创建在 MainActivity 上未调用 onCreate 的问题,如果onCreate 没有被调用,那么我将如何获取意图的价值。
    • 我应该将值放在正常意图还是待定意图上?请提出建议。
    • 您提供的标志是 Intent 标志,应该在 Intent 上设置; see the documentationonCreate() 没有被调用,因为活动已经创建并且仍在运行。您可以改为观察onNewIntent(),如described in the documentation here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多