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

时间:2018-10-05 05:06:46

标签: android amazon-web-services amazon-sns

我正在从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 = test message}

    
  

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

欢呼

3 个答案:

答案 0 :(得分:1)

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

    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将从服务器的通知有效负载中获取数据,并在设备上生成通知。

答案 1 :(得分:0)

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

 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());

答案 2 :(得分:0)

结果证明这与主体的格式有关,而不是>>> SNS“ JSON消息生成器”所建议的

主体应采用以下格式:

paragraph = "abaabbccchsjieiaaavdh"
strings = ["aa", "ab"]
strings_in_para = []
for string in strings:
    paragraph_copy = paragraph
    while string in paragraph_copy:
        paragraph_copy = paragraph_copy.replace(string, "", 1)
        strings_in_para.append(string)