2017-03-03 121 views
7

我有一個應用程序應該顯示頭像通知,而應用程序在前臺和後臺(不在歷史中)。如何使用Firebase Cloud Messaging(Notification)在android中使用Heads Up通知,而應用程序不在前臺?

在前景的情況下,我通過以下方法實現了這一點。

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx); 
    builder.setFullScreenIntent(pendingIntent,true); 

但在後臺,它總是在通知顯示area.Can有人告訴如何達致這在後臺(而不是在歷史上)

我嘗試了通知,但不工作的下面的參數。

{ 
    "to" : "_Token_", 
    "priority":"high", 
    "data": 
    { 
     "title": "Test", 
     "text": "Fcm" 
    }, 
    "notification": 
    { 
     "title": "Test", 
     "text": "Fcm" 
    } 
} 
+0

嗨。通過「*背景(不在歷史中)*」,你的意思是應用程序*被殺害*? –

回答

2

Firebase允許在使用數據消息時進行通知自定義。即使在背景中的應用程序中,數據消息也會調用onMessageReceived()方法,以便創建自定義通知。

你可以把這個鏈接引用更多地瞭解數據的通知 Firebase notifications

+0

但是,如果我們使用數據消息,則在我們從後臺清除應用程序後,我們無法獲得通知。 – SachinS

+0

即使我們從最近的應用程序中刪除應用程序,也會收到數據消息通知 – Neha

+0

我嘗試了通知消息和數據消息,但它不工作。文檔中提到「當應用程序處於後臺時,Android會將通知消息引導至系統托盤。用戶點擊通知會默認打開應用程序啓動器「。 – SachinS

0

而不是使用setFullScreenIntent(),試試這個:

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx); 
builder.setPriority(NotificationCompat.PRIORITY_MAX); 
builder.setSound(URI_TO_SOUND); 
builder.setVibrate(ARRAY_WITH_VIBRATION_TIME); 

編輯:背景你應該做呈三角。爲了顯示擡頭通知,應該結合高優先級+聲音和/或振動(最好是兩者)。

EDIT2:你最好在onMessageReceived顯示此通知()方法,在這裏:https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived

0

你應該完全從您的JSON刪除通知的一部分,只是使用的數據!這就是訣竅。 我的意思是這樣的:

{ 
"to" : "_Token_", 
"priority":"high", 
"data": 
{ 
    "title": "Test", 
    "text": "Fcm" 
} 
} 

當你在你的Json通知標記,有時圖書館決定如何處理通知本身。所以當你的應用程序在前臺它不會打電話給你的消息接收器,它會顯示通知本身。

只需刪除通知並始終使用數據標記。

它的工作原理都當你的應用程序是在前景/背景或死亡,停止

0
{ 
    "to":"push-token", 
    "priority": "high", 
    "notification": { 
     "title": "Test", 
     "body": "Mary sent you a message!", 
     "sound": "default", 
     "icon": "youriconname" 
    } 
} 

檢查了這一點:Create Heads-Up Display

+0

歡迎來到Stack Overflow!雖然這可能會在理論上回答這個問題,[這將是更可取的](// meta.stackoverflow.com/q/8259)在這裏包括答案的基本部分,並提供參考鏈接。 –

0

您需要添加監聽器服務,就像在一個標準的GCM實施。


             
  
public class MyGcmListenerService extends GcmListenerService { 
 

 
     private static final String TAG = "MyGcmListenerService"; 
 

 
     /** 
 
     * Called when message is received. 
 
     * 
 
     * @param from SenderID of the sender. 
 
     * @param data Data bundle containing message data as key/value pairs. 
 
     *    For Set of keys use data.keySet(). 
 
     */ 
 
     // [START receive_message] 
 
     @Override 
 
     public void onMessageReceived(String from, Bundle data) { 
 
      String message = data.getString("message"); 
 
      Log.d(TAG, "From: " + from); 
 
      Log.d(TAG, "Message: " + message); 
 

 
      if (from.startsWith("/topics/")) { 
 
       // message received from some topic. 
 
      } else { 
 
       // normal downstream message. 
 
      } 
 

 
      // [START_EXCLUDE] 
 
      /** 
 
      * Production applications would usually process the message here. 
 
      * Eg: - Syncing with server. 
 
      *  - Store message in local database. 
 
      *  - Update UI. 
 
      */ 
 

 
      /** 
 
      * In some cases it may be useful to show a notification indicating to the user 
 
      * that a message was received. 
 
      */ 
 
      sendNotification(message); 
 
      // [END_EXCLUDE] 
 
     } 
 
     // [END receive_message]

然後,註冊您在AndroidManifest.xml標籤接收器監聽收到通知時:

<!-- [START gcm_listener] --> 
    <service 
     android:name="gcm.play.android.samples.com.gcmquickstart.MyGcmListenerService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <!-- [END gcm_listener] --> 

這樣 - 你不必單獨處理傳入的消息情況下,當應用程序在前臺vs背景。

https://developers.google.com/cloud-messaging/

相關問題