Android 单击通知不启动应用程序
当应用程序未运行时收到推送通知且用户按下通知时,推送通知将消失,日志显示:Android 单击通知不启动应用程序,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,当应用程序未运行时收到推送通知且用户按下通知时,推送通知将消失,日志显示: 2019-10-22 12:42:45.747 23260-23260/de.app.test.staging E/FirebaseMessaging: Notification pending intent canceled 以下是应作为启动器活动启动的Splash活动: <activity android:name="de.app.test.login.SplashActivity"
2019-10-22 12:42:45.747 23260-23260/de.app.test.staging E/FirebaseMessaging: Notification pending intent canceled
以下是应作为启动器活动启动的Splash活动:
<activity
android:name="de.app.test.login.SplashActivity"
android:screenOrientation="portrait"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这里会出现什么问题?放弃应用程序:
您需要一个悬挂式帐篷才能打开应用程序
试试这个:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
如果活动处于后台或已关闭,则应用程序启动器活动的通知中心将显示通知消息
您可以使用BroadcastReceiver类。当你关闭应用程序时,广播可以收听此操作。因此,如果您创建一个BroadcastReceiver类,您就不会遇到这个问题
public class ClosedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, HomePage.class));
} else {
context.startService(new Intent(context, HomePage.class));
}
}
}
更多信息:
您需要在notificationBuilder实例中设置PendingContent对象,如
mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Intent intent = new Intent(context, TargetActivity.class));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
请注意,CHANNEL_ID用于构建版本大于或等于Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CHANNEL_ID = createNotificationChannel(CHANNEL_ID, "channelName");
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId, String channelName) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String description = "description";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
mNotificationManager.createNotificationChannel(channel);
return channelId;
}
对于任何正在寻找答案的人,后端会将“单击操作”发送到通知,因此没有针对该活动的意图过滤器 对我来说,点击操作是“打开活动1”,所以我只在我的SplashActivity中添加了一个意图过滤器,如下所示:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
如您所见,尝试将导出添加为true,但没有帮助。共享挂起的意图代码,并且此问题仅在PIE上出现。或者可能是您还没有在清单中配置firebase messaging,现在无法在Pie下面的设备上试用,将在白天看到。当应用程序处于前台时,它就可以工作了。问题是应用程序处于后台时(没有进程运行)。你是对的。答案已编辑@萨瓦迪米特里耶维奇感谢你的回答,但这是问题所在。launcher活动根本没有启动,日志上写着“Notification pending intent Cancelled”,我应该在哪里注册此广播,为什么只有当设备位于Oreo上方时才注册?请参考此链接,当应用程序位于前台时,此链接才有效。问题在于应用程序处于后台时(没有进程运行)。