单击推送消息后访问推送通知内容(Android)

时间:2015-10-16 09:25:19

标签: java android notifications google-cloud-messaging push

我有一个Android应用,它正在接收推送消息。

如果用户点击推送消息,则调用活动GoToNotification。 到目前为止这是有效的。

但是如何访问推送消息的内容? Bundle.extra? 我正在尝试,对推送消息的某些内容作出反应,但捆绑额外总是为空。

到目前为止我的代码:

private void sendNotification(String msg, Bundle extras) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, GoToNotification.class), 0);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("Warning")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setExtras(extras)
                    .setContentText(msg);

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


public class GoToNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            System.out.println(extras.toString());
        } else {
            System.out.println("extras is null");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Intent notificationIntent = new Intent(context, SampleActivity.class);
notificationIntent.putExtra("EXTRA_ID", "ur data");

和你的活动

if (extras != null) {
 ID = extras.getString("EXTRA_ID");
}

答案 1 :(得分:0)

谢谢,我设法解决了问题,这里是代码:

如上所述,我需要添加额外的内容。 设置requestId也很重要,没有它就无法工作。

以下代码正在运行:

private void sendNotification(String msg, String dpId) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, GoToNotification.class);
    intent.putExtra("dpId", dpId);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    int requestID = (int) System.currentTimeMillis();
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,
            intent, 0);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("Test")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(Integer.parseInt(dpId), mBuilder.build());
}

public class GoToNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState); String regid;

       Intent intent = getIntent();
       if (intent != null) {
            if (intent.hasExtra("dpId")) {
                 System.out.println("Klick auf push dpid:" + intent.getStringExtra("dpId"));

            }
        }
    }
}

清单:

  <activity android:name=".GoToNotification" android:label="@string/app_name" />