【发布时间】:2017-04-29 05:34:11
【问题描述】:
我尝试从 gcm 推送通知中获取 message 和 title 到活动,但它不起作用。下面是我的代码:
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();
谢谢!
【问题讨论】:
-
把你的活动代码放在这里
-
公共类 NotificationActivity 扩展 AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); Toast.makeText(this, "message"+ getIntent().getStringExtra("message"), Toast.LENGTH_LONG).show(); }
-
在问题中发布 json
标签: android google-cloud-messaging