[英]Notification click onNewIntent not called
This is how activity declared in Manifest
这是Manifest
声明活动的方式
<activity
android:name=".view.activity.HomeActivity"
android:exported="true"
android:launchMode="singleTop"
android:label="@string/app_name"
android:theme="@style/AppTheme.Toolbar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And this is how I generate the notification 这就是我生成通知的方式
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val channel = NotificationChannel("1", "MyApp", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
val notificationLayout = RemoteViews(applicationContext.packageName, R.layout.notification_view)
val notificationLayoutExpanded = RemoteViews(applicationContext.packageName, R.layout.notification_big_view)
updateLayout(notificationLayout, notificationLayoutExpanded, data)
val intent = Intent(applicationContext, HomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
putExtra("Id", Id)
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notification = NotificationCompat.Builder(applicationContext, "1")
.setContentTitle("MyApp")
.setSmallIcon(R.drawable.circle)
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
notificationManager.notify(1, notification.build())
But when I tap on the notification onNewIntent
called sometimes only (Maybe if app in open state, I can't guess correctly). 但是当我点击通知onNewIntent
有时仅被调用时(也许如果应用处于打开状态,我就无法正确猜测)。
onNewIntent
is only called if the activity is already open, It won't be called if the Activity is not open. 仅在活动已打开时才调用onNewIntent
,如果活动未打开则不会调用。
This means that if your activity was closed, it will start normally and onCreate
will be called. 这意味着,如果您的活动已关闭,它将正常启动并调用onCreate
。 You can pass a flag in your intent that tells your Activity that it was started from notification, then you find that flag in your onCreate
and if it exists, you call the same functions as you did in onNewIntent()
: 您可以在意图中传递一个标志,告诉您的活动它是从通知开始的,然后在onCreate
找到该标志,如果存在,则调用与onNewIntent()
相同的函数:
val intent = Intent(applicationContext, HomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
putExtra("Id", Id)
putExtra("notificationIntent", true);
}
And in your OnCreate()
: 在您的OnCreate()
:
boolean isNotificationIntent = getIntent().getExtras().getBoolean("notificationIntent", false);
if(isNotificationIntent){
DoSameWorkAsOnNewIntent();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.