android API评估26中未收到通知

时间:2018-09-28 10:47:53

标签: android firebase

我尝试在Android Oreo中接收通知。但是App没有收到任何

通知。我还创建了Chanel通知,但它不起作用

如果我从fcm发送通知,则收到应用程序。但使用令牌应用程序未收到任何通知。在其他较低版本中,版本通知工作正常。在奥利奥(Oreo)中不起作用。

这是我的MyFirebaseMessagingService类:

     public class MyFirebaseMessagingService extends FirebaseMessagingService {
     public static int NOTIFICATION_ID = 1;
     public static final String NOTIF_CHANNEL_ID = "my_channel_01";


     @Override
     public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        sendNotification(remoteMessage.getData());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotifChannel(this);
        }

     }

     @RequiresApi(api = Build.VERSION_CODES.O)
     private void createNotifChannel(MyFirebaseMessagingService 
     myFirebaseMessagingService)
     {
        NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID,
                "MyApp events", NotificationManager.IMPORTANCE_LOW);
        // Configure the notification channel
        channel.setDescription("MyApp event controls");

        channel.setShowBadge(false);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationManager manager = getApplicationContext().
        getSystemService(NotificationManager.class);

        manager.createNotificationChannel(channel);
        Log.d(TAG, "createNotifChannel: created=" + NOTIF_CHANNEL_ID);
        }

        private void sendNotification(Map<String, String> data) {

        int num = ++NOTIFICATION_ID;
        Bundle msg = new Bundle();
        for (String key : data.keySet()) {
            Log.e(key, data.get(key));
            msg.putString(key, data.get(key));
        }
        Intent intent = new Intent(this, HomeActivity.class);
        if (msg.containsKey("action")) {
            intent.putExtra("action", msg.getString("action"));
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* 
        Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = 
        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new 
         NotificationCompat.Builder(this)
                .setSmallIcon(instauser.application.apps.R.drawable.icon)
                .setContentTitle(msg.getString("title"))
                .setContentText(msg.getString("msg"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

         NotificationManager notificationManager =
                (NotificationManager) 
         getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(num, notificationBuilder.build());
    }

} 

我还创建了一个通知频道,但它不起作用

1 个答案:

答案 0 :(得分:2)

您创建了通知频道,但未将其设置为通知频道

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        ...
        .setChannelId(NOTIF_CHANNEL_ID)