首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让onNewIntent()触发

无法让onNewIntent()触发
EN

Stack Overflow用户
提问于 2020-04-30 05:35:56
回答 1查看 125关注 0票数 1

所以我已经解决了7个以上的堆栈溢出问题,并找到了解决这个onNewIntent()的方法,但是即使这样,我似乎还是不能让它工作。

我想触发onNewIntent()的活动:

代码语言:javascript
复制
<activity android:name=".View.Activities.ViewStatus"
        android:launchMode="singleTop"/>
    <activity

在ViewStatus活动中:

代码语言:javascript
复制
@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()没有被触发:

代码语言:javascript
复制
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);

我是不是漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2020-04-30 15:56:00

根据这篇文章,从singleTop改为singleTask拯救了这种情况:Android "single top" launch mode and onNewIntent method

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61512129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档