Android 推送通知记录 onMessageReceived 但未在设备上显示

Android push notification logs onMessageReceived but not displaying on device

我正在从 AWS 向 android 发送一些推送通知,

通知进程正在注册设备,我确实收到了测试通知,但只显示在日志上,而不是像其他任何通知一样显示在屏幕顶部的通知栏上...

 public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.

    Log.d("mako", "A From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d("mako", "B Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long-running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
//                scheduleJob();

        } else {
            // Handle message within 10 seconds
//                handleNow();

        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d("mako", "C Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

所以,我发送测试通知:

{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}

我可以在控制台日志中看到消息测试:

10-05 14:57:08.827 23062-23296/com.sb.comm D/mako: B Message data payload: {message=test message}

但是在小弹出窗口中没有显示,缺少什么来在屏幕上显示实际的推送通知?

干杯

请添加这些代码行以在通知栏上显示通知。

 Intent intent = new Intent(this, YourActivity.class);
 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
 PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notificationBuilder = new 
    NotificationCompat.Builder(this, "my_chanel_id");
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher_app);
    notificationBuilder.setContentTitle("Titlee");
    notificationBuilder.setContentText("anyText");
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(uri);
    notificationBuilder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel("mychanelId", 
           "hyperlocal", importance);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(message_id, notificationBuilder.build());

您必须像这样在您的设备上生成通知

    public void onMessageReceived(RemoteMessage remoteMessage) {

      NotificationCompat.Builder notificationBuilder = 
         new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("App Name")
        .setBadgeIconType(R.drawable.ic_notification)
        .setLargeIcon(BitmapFactory.decodeResource(
         getResources(),R.drawable.ic_notification))
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
         .setContentText(remoteMessage.getData().get("body"));



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

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

此处 Notification Builder 将从您的服务器通知负载获取数据并在设备上生成通知。

原来是body格式的问题,不是>>> SNS建议的"JSON message generator"

正文应采用以下格式:

{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}