Android 12和13中,为了保护用户的隐私,系统对后台执行的一些操作进行了限制,导致在ionic5的App后台发送Firebase推送通知时会出现问题。为了解决这个问题,可以按照以下步骤进行操作:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
NotificationChannel channel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
// Start the service
Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class);
startForegroundService(serviceIntent);
} else {
// Start the service
Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class);
startService(serviceIntent);
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//处理推送通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_notification);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notificationBuilder.build());
Intent intent = new Intent