【问题标题】:Get message and title From Push Notification GCM to activity从 Push Notification GCM 获取消息和标题到活动
【发布时间】:2017-04-29 05:34:11
【问题描述】:

我尝试从 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();

谢谢!

【问题讨论】:

  • 把你的活动代码放在这里
  • 公共类 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


【解决方案1】:

试试这个:

//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();

}

添加以上条件

【讨论】:

  • 我试过了,但它显示如下 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a空对象引用
  • 输入您的 json...输入您的 GCMintentservice 完整代码...您在推送通知中收到正确的消息吗?
  • 请检查我更新的答案,以免您的应用程序崩溃
猜你喜欢
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 2016-07-24
  • 2014-07-25
  • 2014-10-28
  • 1970-01-01
相关资源
最近更新 更多