20

我正在使用Firebase雲消息傳遞發送推送通知。當應用程序處於後臺或未運行時,推送通知無法正常工作

這是我FirebaseMessageService

public class FireBaseMessageService extends FirebaseMessagingService { 


@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.e("TAG", "From: " + remoteMessage.getFrom()); 
    Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+" : "+remoteMessage.getData().get("CardCode")); 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 


private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, StartActivity.class); 
    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_final) 
      .setContentTitle("Notification") 
      .setContentText(messageBody) 
      .setTicker("Test") 
      .setAutoCancel(true) 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setContentIntent(pendingIntent); 

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

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

而且FirebaseInstanceServer

public class FirebaseInstanceService extends FirebaseInstanceIdService { 


@Override 
public void onTokenRefresh() { 
    // Get updated InstanceID token. 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.e("TAG", "Refreshed token: " + refreshedToken); 

    // TODO: Implement this method to send any registration to your app's servers. 
    sendRegistrationToServer(refreshedToken); 

} 

private void sendRegistrationToServer(String token) { 


     // Add custom implementation, as needed. 
     Log.e("TAG", "Refreshed token2: " + token); 
    } 
} 

這是在AndroidManifest聲明:

<service 
    android:name=".util.notifications.FireBaseMessageService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
    </intent-filter> 
</service> 

<service 
    android:name=".util.notifications.FirebaseInstanceService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
    </intent-filter> 
</service> 

所以問題是,當應用程序正在運行ticker是表現良好,通知com e默認聲音,但是當App處於前臺或未運行通知時,沒有任何聲音,並且ticker未顯示在狀態欄中。
爲什麼會發生這種情況,我該如何解決?

回答

47

使用FCM,您可以指定一個POST有效負載發送到https://fcm.googleapis.com/fcm/send。在該有效載荷中,您可以指定一個datanotification密鑰或兩者。

如果您的有效負載只包含一個data鍵,您的應用程序將自行處理所有推送消息。例如。他們全部被送到你的onMessageReceived處理程序。

如果您的有效載荷包含notification鍵,則您的應用程序將自己處理推送消息本身只有如果您的應用程序處於活動狀態/在前臺。如果不是(所以它在後臺,或完全關閉),FCM處理通過使用您輸入notification密鑰負載的值顯示通知。

請注意,從控制檯(如Firebase控制檯)發送的通知,它們始終包含notification密鑰。

看起來好像你想自己處理FCM消息,所以你可以自定義通知多一點等,所以最好不要在0123有效載荷中包含notification密鑰,這樣所有的推送消息都會傳遞給你onMessageReceived

你可以閱讀更多關於它在這裏:
Advanced messaging options
Downstream message syntax

+1

謝謝,你是對的,如果我刪除了'notification'關鍵,我開始收到通知的正確 – Ololoking

+0

蒂姆是正確的,我只是想補充一點,從火力地堡控制檯發送的消息是通知消息,因此如果您使用Firebase控制檯,則必須相應地處理它們。 –

+0

感謝它幫助我很多。 –

相關問題