获取消息和标题从推送通知GCM到活动

时间:2017-04-29 05:34:12

标签: android google-cloud-messaging

我尝试从活动的gcm推送通知中获取messagetitle,但这不起作用。这是我的代码:

GcmIntentService.java类中的sendNotification

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

        Intent intent = new Intent(this, NotificationActivity.class);
        intent.putExtra("message", msg);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Log.e("message", msg);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent , 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setContentTitle(getResources().getString(R.string.app_name))
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);


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

NotificationActivity.java类

Toast.makeText(this, "message"+ getIntent().getStringExtra("message"), Toast.LENGTH_LONG).show();

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

//onCreate() or onNewIntent() method
bundle = getIntent().getExtras();
if (bundle != null && bundle.getString("message") != null) {
    Toast.makeText(this, "message"+ getIntent().getExtra().getString("message"), Toast.LENGTH_LONG).show();

}

添加以上条件