简体   繁体   English

无法获取 onNewIntent() 触发

[英]Can't get onNewIntent() to trigger

so i've gone through over 7 stackoverflow questions with solutions to solving this onNewIntent() but even after all that I cant seem to get it to work.所以我已经通过解决这个 onNewIntent() 的解决方案解决了超过 7 个 stackoverflow 问题,但即使如此,我似乎仍然无法让它工作。

The activity that I want to trigger onNewIntent():我要触发 onNewIntent() 的活动:

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

In the ViewStatus 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_);

}

I fire up the notification but on clicking notification my onNewIntent() isn't triggered:我启动了通知,但是在单击通知时我的 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);

Is there something I'm missing?有什么我想念的吗?

Changing from singleTop to singleTask saved the situation according to this post: Android "single top" launch mode and onNewIntent method根据这篇文章从singleTop更改为singleTask保存了情况: Android "single top" launch mode and onNewIntent method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM