1

FCMで送信者IDを使用して通知を受け取る方法。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()); 
    } 
} 
+1

Firebaseの設定に問題がある必要があります。 Androidスタジオからクラウドメッセージングを設定するには、次の手順に従います。 'ツール> Firebase>クラウドメッセージング>セットアップFirebaseクラウドメッセージング' – Wizard

答えて

1

あなたがIT-あなたがonMessageReceived()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()); 
} 
0

を使用したいsendNotificationを()メソッドを呼び出してください。このメソッドを呼び出して通知を送信します。

-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()); 
} 
+0

おそらくよりよい答えのためにコード全体を表示しますか? – Jensd

+0

@Jensdありがとう私は私のansを更新しました –

関連する問題