我尝试在应用被终止时发送推送通知,但设备仅在应用启动时才接收通知。
我有:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
并用于创建通知:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(smallIconRes)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setColor(ContextCompat.getColor(context, notificationColor))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(id, notificationBuilder.build());
}
Firebase函数可以做到这一点:
var options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
const payload = {
"notification":{
"title":`${notifiTitle}`,
"body":`${notifiBody}`,
"click_action":".MainActivity",
"sound":"default"
}
};
return admin.messaging().sendToDevice(snapshot.val(), payload, options);
我尝试使用Android 8.1.0的“联想Moto G5S Plus XT1805”,但没有问题。设备在应用关闭和打开时均会接收通知。好!
问题出在其他设备上,即“ Lenovo Tablet TB-X103X”,Android 7.0。 仅当应用启动时,此设备才会接收通知!当应用被杀死时,不会接收通知。我等待1个小时,打开应用程序,然后收到通知。我不知道DOZE模式是否有问题,我发送带有Firebase函数的HIGH优先级通知。或者它是平板电脑的联想配置。
我尝试添加请求权限:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
但没有效果!
有人可以帮我吗?
谢谢!