1

So erhalten Sie eine Benachrichtigung mithilfe der Absender-ID in FCM.So erhalten Sie eine Benachrichtigung mithilfe der Absender-ID in FCM

Ich versuche, eine Benachrichtigung mit Absender-ID in FCM zu erhalten, und es hat auch die Geräte-ID und die serverseitige Sendung ist erfolgreich, aber ich kann die Benachrichtigung nicht erhalten. Wie man das Problem löst. Ich habe viel geforscht und finde keine Lösung. Unten ist mein Code.

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

Es muss ein Problem mit der Firebase-Konfiguration geben. Befolgen Sie diese Schritte, um Cloud Messaging von Android Studio einzurichten. 'Tools> Firebase> Cloud Messaging> Firebase-Cloud-Messaging einrichten ' – Wizard

Antwort

1

Bitte rufen Sie sendNotification() -Methode, wo Sie it-

@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

Sie sendNotification() Methode in onMessageReceived() nennen vergessen verwenden möchten. Rufen Sie diese Methode auf, um eine Benachrichtigung zu senden.

-1

Sie vergessen, sendNotification() Methode aufzurufen.

@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

Vielleicht zeigen Sie den gesamten Code für eine bessere Antwort? – Jensd

+0

@Jensd danke ich habe meine ANS aktualisiert –

Verwandte Themen