来自待处理 Intent 的 Android bundle 始终接收为 null

标签 android push-notification notifications android-pendingintent

我尝试了 stackOverflow 中建议的所有内容 - 没有任何帮助。
通知工作正常,只是以某种方式丢失了它的 bundle 。

这是我的 PendingIntent:

 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            Intent intent = new Intent(this, LoginActivity.class);
         //   Intent intent = new Intent().setClass(this, LoginActivity.class);
            intent.putExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION,true);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


            //intent.setAction("dummy");

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

            NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(text)
/*                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(message))*/
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent);

            notificationManager.notify(0, noBuilder.build());

1) 向 Intent 添加 Action :intent.setAction("dummy");

2) 将 setClass 添加到 Intent: .setClass(this, LoginActivity.class);

3)待定 Intent 尝试了所有标志:PendingIntent.FLAG_UPDATE_CURRENT/FLAG_CANCEL_CURRENT/FLAG_ONE_SHOT/0

4) 尝试在“onNewIntent”和“onCreate”中接收额外内容

接收 Activity 中的额外内容总是错误的
 @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "im in login from notification!   "+ intent.hasExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION));
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Intent intent = getIntent();
    Log.d(TAG, "im in login from notification!   "+ intent.hasExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION));

}

有任何想法吗?
谢谢!

最佳答案

onNewIntent添加:setIntent(intent)

关于来自待处理 Intent 的 Android bundle 始终接收为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372978/

相关文章:

reactjs - FCM(Firebase 云消息): Firebase push notifications are displayed in Firefox but not in Chrome

iOS "Local"推送通知

rspec - 如何关闭新输出的 iTerm2 growl 通知,但保留它们用于 rspec 结果

android - 如何在通知中添加声音?

javascript - 当浏览器在后台运行时收到通知时使用 Firebase 播放声音

android - CountDownTimer 没有取消

java - 如何实现复杂的动画?

java - SQLite 数据库查询只返回一行

java - Android:下载文件代码不起作用

javascript - gcm Meteor 中的多重通知