【问题标题】:How to receive notification using sender id in FCM如何在 FCM 中使用发件人 ID 接收通知
【发布时间】:2017-12-13 06:56:50
【问题描述】:

如何在 FCM 中使用发件人 ID 接收通知。

我正在尝试使用 FCM 中的发件人 ID 接收通知,它还具有设备 ID,并且服务器端发送成功,但我无法收到通知。如何解决问题。我做了很多研究,找不到任何解决方案。下面是我的代码。

java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        if ( true) {
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}
// [END receive_message]

/**
 * Schedule a job using FirebaseJobDispatcher.
 */
private void scheduleJob() {
    // [START dispatch_job]
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new 
    GooglePlayDriver(this));
    Job myJob = dispatcher.newJobBuilder()
            .setService(MyJobService.class)
            .setTag("my-job-tag")
            .build();
    dispatcher.schedule(myJob);
    // [END dispatch_job]
}

/**
 * Handle time allotted to BroadcastReceivers.
 */
private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
}

/**
 * Create and show a simple notification containing the received FCM 
 message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, 
    notificationBuilder.build());
  }
}

【问题讨论】:

  • Firebase 配置肯定有问题。要从 Android Studio 设置云消息传递,请按照以下步骤操作。 Tools>Firebase>Cloud Messaging>Setup Firebase cloud messaging

标签: android firebase firebase-cloud-messaging android-notifications firebase-job-dispatcher


【解决方案1】:

请在您想使用它的地方调用 sendNotification() 方法-

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        if ( true) {
            scheduleJob();
            sendNotification("YourMessage")

        } else {
            // Handle message within 10 seconds
            handleNow();
        }
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}
// [END receive_message]

/**
 * Schedule a job using FirebaseJobDispatcher.
 */
private void scheduleJob() {
    // [START dispatch_job]
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    Job myJob = dispatcher.newJobBuilder()
            .setService(MyJobService.class)
            .setTag("my-job-tag")
            .build();
    dispatcher.schedule(myJob);
    // [END dispatch_job]
}

/**
 * Handle time allotted to BroadcastReceivers.
 */
private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
}

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

【讨论】:

    【解决方案2】:

    您忘记在onMessageReceived() 中调用sendNotification() 方法。调用此方法发送通知。

    【讨论】:

      【解决方案3】:

      您忘记调用sendNotification() 方法。

      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          Log.d(TAG, "From: " + remoteMessage.getFrom());
          // Check if message contains a data payload.
          if (remoteMessage.getData().size() > 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
              if ( true) {
                  scheduleJob();
                  sendNotification("YourMessage")
      
              } else {
                  // Handle message within 10 seconds
                  handleNow();
              }
          }
          // Check if message contains a notification payload.
          if (remoteMessage.getNotification() != null) {
              Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
          }
      }
      // [END receive_message]
      
      /**
       * Schedule a job using FirebaseJobDispatcher.
       */
      private void scheduleJob() {
          // [START dispatch_job]
          FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
          Job myJob = dispatcher.newJobBuilder()
                  .setService(MyJobService.class)
                  .setTag("my-job-tag")
                  .build();
          dispatcher.schedule(myJob);
          // [END dispatch_job]
      }
      
      /**
       * Handle time allotted to BroadcastReceivers.
       */
      private void handleNow() {
          Log.d(TAG, "Short lived task is done.");
      }
      
      /**
       * Create and show a simple notification containing the received FCM message.
       *
       * @param messageBody FCM message body received.
       */
      private void sendNotification(String messageBody) {
          Intent intent = new Intent(this, MainActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
      
          String channelId = getString(R.string.default_notification_channel_id);
          Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder =
                  new NotificationCompat.Builder(this, channelId)
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setContentTitle("FCM Message")
                          .setContentText(messageBody)
                          .setAutoCancel(true)
                          .setSound(defaultSoundUri)
                          .setContentIntent(pendingIntent);
      
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        • 2017-11-16
        • 1970-01-01
        • 2023-02-25
        • 2019-08-31
        • 2020-11-27
        • 1970-01-01
        相关资源
        最近更新 更多