2017-09-25 112 views
0

我使用fcm和擡頭通知將顯示應用程序何時打開,但未顯示應用程序未打開或已關閉時是否顯示。 如何處理應用程序未打開時的通知?當應用程序未打開時顯示擡頭通知

+0

到目爲止我知道,當消息被收到並且應用程序在後臺時,FCM會自動顯示通知。 –

+0

是正常通知顯示,但擡頭通知不顯示。 –

回答

0

文件說:

藉助Android 5.0(API級21),通知可以在一個小 浮動窗口(也稱爲擡頭通知)時,該設備 是活性出現(即,設備已解鎖並且其屏幕處於打開狀態)。 這些通知與您的 通知的緊湊形式類似,但擡頭通知也顯示動作 按鈕。用戶可以在沒有 而離開當前應用程序的情況下采取行動或解除提醒通知。

按照Doc,如果你想單挑,你必須創建自己的如下通知:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH); 
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]); 

不要濫用擡頭通知。見here何時使用擡頭通知:

MAX:對於提醒用戶一個 條件是時間緊急和需要解決才 可以與特定的持續重大而緊迫的通知任務。

HIGH:主要用於重要的溝通,如消息或聊天 內容對用戶特別感興趣的事件。 高優先級通知觸發擡頭通知顯示。

附加說明從HERE

更新:

要覆蓋GCM監聽服務:

<service android:name=".MyGcmListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
</service> 

FCM是:

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

然後重寫方法:

GCM:

public class MyGcmListenerService 
     extends GcmListenerService { 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    ... create your heads-up notification here. 
} 

FCM:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */  
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    ... create your heads-up notification here. 
} 
+0

我試試這個,但是當應用程序沒有打開。僅在系統托盤中顯示通知並單獨顯示通知。 –

+0

@FakeTonT是的,你必須覆蓋FCM/GCM的現有接收器。默認通知不顯示爲擡頭。 – james

+0

@FakeTonT看到我更新的答案。 – james

0

不能在評論後,所以在這兒呢。嘗試一下,我已經測試過:

private void test() { 
    Intent intent; 
    intent = new Intent(this, SplashScreenActivity.class); 
    Bundle bundle = new Bundle(); 
    bundle.putBoolean("isDisplayAlert", true); 
    bundle.putString("NOTIFICATION_DATA", "data"); 
    intent.putExtras(bundle); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 
      new Random().nextInt(), intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_location) 
      .setContentTitle("Title") 
      .setContentText("Body") 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setOnlyAlertOnce(true) 
      .setFullScreenIntent(pendingIntent, true); 

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

    notificationBuilder.setPriority(Notification.PRIORITY_HIGH); 
    notificationBuilder.setVibrate(new long[0]); 
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
+0

我通過控制檯firebase進行測試並拍攝到我的手機或emu。它同樣發生了擡頭通知不顯示,但只顯示正常通知。 –

+0

顯示您的代碼,完整的流程。 – james

+0

https://imgur.com/a/rHc72我的代碼和清單https://imgur.com/a/3odHb –

相關問題