FCM推送通知不适用于某些设备



我们已经创建了一个使用FCM通知的聊天应用程序,我的代码也正确,我的设备也可以获取推送通知数据,但是某些中国制造的设备,例如Vivo,Oppo,Oppo,One Plus,小米是除非我在受保护的应用程序列表中添加应用程序列表,否则不允许通知显示。是他们的任何解决方案。

https://hackernoon.com/notifications-in-android-are-horroken-broken-b8dbec63f48a

https://github.community/t5/project-development-help-and/firebase-push-push-notification-not-not-not-receiving-on-some-android-devices/td-pd-p/5489

private NotificationManagerCompat notificationManager;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("test","call");
    notificationManager = NotificationManagerCompat.from(this);
    sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
private void sendNotification(String title, String msg) {
    Intent resultIntent = new Intent(this, ActivitySplashScreen.class);
    String channelId = getString(R.string.chc);
    String channelName = "Message Notification";
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(99, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setContentText(msg)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setContentIntent(pendingIntent)
            .build();
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager manager = getSystemService(NotificationManager.class);
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
    }
    notificationManager.notify(10, notification);
}

我可以确认此实现与一个Plus和小米设备效果很好(我们有大量的用户使用该设备的应用程序,并且在FCM通知上没有从中创建崩溃或问题(。

无法确认Vivo或Oppo的任何内容(到目前为止,我们知道我们没有使用此类设备的用户(。

最重要的是,如果有关应用程序在前台或背景中,则可以很好地实现通知功能。如果有人想对此事进行缩放,本文以一种简单的方式解释了。

现在,我用于Android中的FCM实现的代码:

//mymessagingservice

public class MyFirebaseMessagingService extends FirebaseMessagingService {
  public static final String TAG = "Firebase Msg";
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    final LocalNotification localNotification = new LocalNotification();
    //data --> entityId - entityType - pushNotificationType
    // Check if message contains a data payload.
    if (data.size() > 0) {
        localNotification.setEntityId(data.get("entityId"));
        localNotification.setEntityType(data.get("entityType"));
        localNotification.setPushNotificationType(data.get("pushNotificationType"));
        localNotification.setTitle(data.get("title"));
        localNotification.setBody(data.get("body"));
        localNotification.setIcon(data.get("icon"));
        localNotification.setDate(new Date().getTime());
        localNotification.setRead(false);
    }
    if (localNotification.getEntityId() != null) {
        LocalNotification notificationRetrieved = FirebaseNotificationsHelper.insertLocalNotification(localNotification);
        FirebaseNotificationsHelper.createNotificationInStatus(notificationRetrieved);
    }
  }
}

//创建通知状态

static void createNotificationInStatus(LocalNotification localNotification) {
    String notificationChannelId = App.getContext().getString(R.string.default_notification_channel_id);
    String notificationChannelName = App.getContext().getString(R.string.default_notification_channel_name);
    String notificationChannelDescription = App.getContext().getString(R.string.default_notification_channel_description);
    NotificationCompat.Builder notificationBuilder;
    NotificationCompat.Builder notificationBuilderPublicVersion;
    notificationBuilder = new NotificationCompat.Builder(App.getContext(), notificationChannelId);
    notificationBuilderPublicVersion = new NotificationCompat.Builder(App.getContext(), notificationChannelId);
    notificationBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_LIGHTS | NotificationCompat.DEFAULT_SOUND)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_stat_push_notif)
            .setTicker(localNotification.getTitle())
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(localNotification.getTitle())
            .setContentText(localNotification.getBody())
            .setStyle(new NotificationCompat.BigTextStyle().bigText(localNotification.getBody()))
            .setPublicVersion(notificationBuilderPublicVersion.setSmallIcon(R.drawable.ic_stat_push_notif).setContentTitle(localNotification.getTitle()).
                    setWhen(System.currentTimeMillis()).setContentText(localNotification.getBody()).build())
            .setGroup(NOTIFICATION_GROUP)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setAutoCancel(true);
    int notificationId = SharedPreferencesUtils.getInstance(App.getContext()).getIntValue(PREF_KEY_NOTIFICATION_ID, 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, notificationChannelName, NotificationManager.IMPORTANCE_HIGH);
        // Configure the notification channel.
        notificationChannel.setDescription(notificationChannelDescription);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
        notificationManager.notify(notificationId, notificationBuilder.build());
    } else {
        /* Kitkat and previous versions don't show notifications using NotificationManagerCompat */
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            NotificationManager notificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationId, notificationBuilder.build());
        } else {
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(App.getContext());
            notificationManager.notify(notificationId, notificationBuilder.build());
        }
    }
    notificationId++;
    SharedPreferencesUtils.getInstance(App.getContext()).setValue(PREF_KEY_NOTIFICATION_ID, notificationId);
}

i Donot认为,这是一个通用的解决方案,因为您的应用程序基于Google-fcm,这意味着Android设备定期与Google-Internet服务相交。

据我所知

相关内容

  • 没有找到相关文章

最新更新