【问题标题】:Can't get onNewIntent() to trigger无法获取 onNewIntent() 触发
【发布时间】:2020-04-29 21:35:56
【问题描述】:

所以我已经通过解决这个 onNewIntent() 的解决方案解决了超过 7 个 stackoverflow 问题,但即便如此,我似乎仍然无法让它工作。

我要触发 onNewIntent() 的活动:

<activity android:name=".View.Activities.ViewStatus"
        android:launchMode="singleTop"/>
    <activity

在 ViewStatus 活动中:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    String author_ = intent.getStringExtra("author");
    String postedTo_ = intent.getStringExtra("postedTo");
    String key_ = intent.getStringExtra("key");

    Log.d(TAG, "onNewIntent: \nauthor: "+author_+"\npostedTo: "+postedTo_+"\nkey: "+key_);

}

我启动了通知,但点击通知时我的 onNewIntent() 没有被触发:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                        //https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
                                        builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
                                                .setGroupSummary(true)
                                                //.setOnlyAlertOnce(true)
                                                .setGroup(String.valueOf(notifId))
                                                .setSmallIcon(R.drawable.logo_notification)
                                                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                                                .setContentTitle(fromUser.getFirstname() + " " + fromUser.getLastname() + " commented:")
                                                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                                                .setSound(soundUri)
                                                .setTimeoutAfter(40000) //40s
                                                .setOnlyAlertOnce(true)
                                                .setContentText(statusUpdate.getStatus())
                                                .setStyle(new Notification.BigTextStyle() //https://developer.android.com/training/notify-user/expanded
                                                        .bigText(statusUpdate.getStatus()));
                                    } else {
                                        builder = new Notification.Builder(getApplicationContext())
                                                .setGroupSummary(true)
                                                //.setOnlyAlertOnce(true)
                                                .setGroup(String.valueOf(notifId))
                                                .setSmallIcon(R.drawable.logo_notification)
                                                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                                                .setContentTitle(fromUser.getFirstname() + " " + fromUser.getLastname() + " commented:")
                                                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                                                .setSound(soundUri)
                                                .setAutoCancel(true)
                                                .setOnlyAlertOnce(true)
                                                .setPriority(Notification.PRIORITY_MAX)
                                                .setContentText(statusUpdate.getStatus())
                                                .setStyle(new Notification.BigTextStyle() //https://developer.android.com/training/notify-user/expanded
                                                        .bigText(statusUpdate.getStatus()));
                                    }

                                }

                                Intent intent = new Intent(ForegroundService.this, ViewStatus.class);
                                intent.putExtra("author", newNotification.getAuthor());
                                intent.putExtra("postedTo", newNotification.getPostedTo());
                                intent.putExtra("key", newNotification.getMessage());
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                PendingIntent contentIntent = PendingIntent.getActivity(ForegroundService.this, notifId, intent,PendingIntent.FLAG_UPDATE_CURRENT);
                                builder.setContentIntent(contentIntent);
                                Notification notification = builder.build();
                                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                notification.defaults |= Notification.DEFAULT_SOUND;
                                notification.icon |= Notification.BADGE_ICON_LARGE;
                                manager.notify(notifId, notification);

我有什么遗漏吗?

【问题讨论】:

  • 活动当时是否已经在运行? onNewIntent() 仅在活动正在运行并且startActivity() 调用解析到该特定活动实例时才被调用。
  • 没有活动没有运行
  • 那是你的问题。一个典型的模式是有一个onCreate()onNewIntent() 都可以调用的通用方法,用于Intent 的通用处理,无论Intent 创建的活动是否只是交付给它。
  • 那么究竟如何处理将新数据传递给通知pendingintent?
  • 我不明白这个问题,对不起。如果您的意思是“传递新数据来自通知pendingintent”,那么我会做我在之前评论中写的:有一个通用方法处理它,从onCreate()onNewIntent()调用.

标签: android android-notifications onnewintent


【解决方案1】:

singleTop更改为singleTask根据这个帖子保存了情况:Android "single top" launch mode and onNewIntent method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多