繁体   English   中英

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

[英]Access the Push notification content after a click on the push message (Android)

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

如果用户单击推送消息,则会调用活动GoToNotification。 到目前为止,这是可行的。

但是,如何访问推送消息的内容? Bundle.extra? 我正在尝试对推送消息的某些内容做出反应,但bundle extra始终为null。

到目前为止,这是我的代码:

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");
        }
    }
}
Intent notificationIntent = new Intent(context, SampleActivity.class);
notificationIntent.putExtra("EXTRA_ID", "ur data");

和我们的活动

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

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

正如已经写的,我需要把额外的内容。 设置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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM