2017-05-25 91 views
2

我正在開發應用程序爲android和ios使用反應原生。當我收到遠程通知時,我想在應用程序圖標上顯示徽章號碼。我正在使用react-native-fcm第三方庫和iOS徽章工作正常。在Android中,我只能在應用處於前景時顯示徽章號碼。當應用程序被殺或在後臺我無法顯示徽章號碼。我知道Android本身不支持顯示徽章,但我已經看過Facebook和Messenger應用程序在Android上顯示徽章。請有人可以告訴我如何實現這一點在Android甚至應用程序被殺死或在後臺。提前致謝。在Android上設置應用程序圖標時的徽章號碼,當應用程序在後臺或遇到類似Facebook應用程序時收到通知

+0

我,就在昨天在應用程序圖標上實現了徽章號碼顯示,但我使用的是FCM。 – Avi

+0

我使用https://github.com/leolin310148/ShortcutBadger庫在應用程序圖標上顯示徽章。 – Avi

+0

是的,react-native-fcm也使用FCM服務。問題是onMessageReceived不會在應用程序被終止或在後臺調用。 – Zwe

回答

2

onMessageReceived沒有得到調用,它只調用數據有效載荷發送。

如果數據有效載荷和通知有效載荷都發送,也比onMessageReceived沒有調用。

使用下面的代碼在應用程序處於後臺時從服務器獲取徽章,或者因爲您的FirebaseMessagingService正在運行而被殺死。

public class Custom_FirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "FirebaseMsgService"; 
    String activityName; 

    @Override 
    public void zzm(Intent intent) { 
     Log.i("uniqbadge", "zzm"); 
     Set<String> keys = intent.getExtras().keySet(); 
     for (String key : keys) { 
      try { 
       Log.i("uniq", " " + key + " " + intent.getExtras().get(key)); 
       if (key.equals("badge")) { 
        String cnt = intent.getExtras().get(key).toString(); 
        int badgeCount = Integer.valueOf(cnt); 
        Log.i("uniq", " badge count " + badgeCount); 
        ShortcutBadger.applyCount(this, badgeCount); 
        Log.i("uniq", " " + "end"); 
       } 
      } catch (Exception e) { 
       Log.i("uniqbadge", "zzm Custom_FirebaseMessagingService" + e.getMessage()); 
      } 
     } 

     super.zzm(intent); 
    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.i(TAG, "From: " + remoteMessage.getFrom()); 

     if (remoteMessage == null) 
      return; 

     if (remoteMessage.getNotification() != null) { 
      Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 

     } 
     if (remoteMessage.getData().size() > 0) { 
      Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString()); 
      ... 
+0

它現在有效!多謝老兄! – Zwe

+0

總是歡迎兄弟。 – Avi

相關問題