当应用程序在前台时未收到通知也未在单击通知时接收有效负载数据

时间:2017-09-24 20:34:13

标签: android firebase firebase-notifications

所以,我正试图在应用程序处于后台或前台时尝试通知并尝试将数据负载从firebase控制台发送到Android应用程序活动,但我得到了这个

  1. 当应用程序处于后台时,我收到通知
  2. 当应用处于前台时未收到通知
  3. 此外,我从firebase控制台获取的datapayload,带有键" str",未在活动中显示
  4. 请告诉我问题在哪里以及如何解决。

    private static final String TAG = "FirebaseMessagingServic";
    
    public FirebaseMessagingService() {
    }
    
    String value = "0";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            value = remoteMessage.getData().get("str"); //key text in quotes
    
        }
        String title = remoteMessage.getNotification().getTitle(); //get title
        String message = remoteMessage.getNotification().getBody(); //get message
    
        sendNotification(title, message, value);
        // Check if message contains a notification payload.
      /*  if (remoteMessage.getNotification() != null) {
    
    
    
            Log.d(TAG, "Message Notification Title: " + title);
            Log.d(TAG, "Message Notification Body: " + message);
    
    
        }*/
    }
    
    @Override
    public void onDeletedMessages() {
    
    }
    
    private void sendNotification(String title,String messageBody, String value) {
        Intent intent = new Intent(this, CS101noti.class);
        SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
        prefs.edit().putString("body", value).apply();
    
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    

1 个答案:

答案 0 :(得分:0)

我已经解决了它:)

您只需要从REST API或Postman发送数据有效负载,并使用Java代码获取数据值, 它会像Charm一样工作:)