push-уведомление моего приложения для Android работает неправильно на всех типах устройств Android - PullRequest
0 голосов
/ 21 января 2019

Я занимаюсь разработкой приложения для Android и пытаюсь реализовать push-уведомление.

Мое приложение работает на Samsung 8, но не работает на Samsung Note 5 под управлением Android 7.0 или OPPO A73 с Android 7.1.1.

Проблема в том, что при получении уведомления на значке приложения не отображается значок, а только воспроизводится звук, но на значке приложения не отображается значок.

Когда я тестирую его с Samsung 5.0, если приложение находится на переднем плане, значок отображается, но отсутствует звук, а если приложение находится на заднем плане, звук работает, но счетчик значков отсутствует.

Вот мой исходный код:

  public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCM Service";

    private PendingIntent pendingIntent;
    private NotificationManager notificationManager;
    private NotificationCompat.Builder notificationBuilder;

    public String titleOfNotification = "";
    public String messageOfNotification = "";


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "Receiver service started: ");

        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        titleOfNotification = remoteMessage.getNotification().getTitle();
        messageOfNotification = remoteMessage.getNotification().getBody();

        createNotificationChannel();
        handleNotification();

    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void handleNotification() {


        Intent intent = new Intent(this, NotificationsListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "chan")
                .setSmallIcon(R.drawable.kamarlogo)
                .setContentTitle(titleOfNotification)
                .setContentText(messageOfNotification)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setBadgeIconType(1)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(0, mBuilder.build());

        Globals.badgeCount = Globals.badgeCount + 1;
        ShortcutBadger.applyCount(this,Globals.badgeCount);





    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "testchannel";
            String description = "test descripion";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("chan", name, importance);
            channel.setDescription(description);
            channel.setShowBadge(true);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }





}
...