2017-07-11 88 views
-2

我已經實施了Firebase,但不幸的是,通知只在應用處於前臺或後臺時工作,但無法在應用關閉時收到任何通知。我嘗試在網上衝浪,但無法獲得任何結果。 即使在關閉它後,有什麼辦法讓應用程序保持活躍狀態​​? 如果是,我認爲這將有助於收到通知。任何有用的建議都受到歡迎。謝謝FCM推送通知在前臺和後臺工作,但在應用關閉時無法工作

+0

顯示代碼是如何實現的。 – james

回答

0

我做的一件事是我不依賴通知響應,而是傳遞數據對象並自己做出自定義通知。

下面是代碼,可以幫助你,因爲我們可以訪問數據,即使對象時,應用程序是開放及關閉的:

Map<String, String> dataMap = remoteMessage.getData(); 
String notif = dataMap.get("title"); 

然後我使用此功能進行通知

private void notificationManager(Context context, String message) { 

    try { 
     long when = System.currentTimeMillis(); 

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

     Log.v("message", "," + message); 

     Intent notificationIntent = new Intent(context, SplashActivity.class); 


     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setAutoCancel(true); 
     builder.setContentTitle(context.getString(R.string.app_name)); 
     builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); 
     builder.setContentText(message); 
     builder.setTicker(message); 
     builder.setLights(Color.GREEN, 500, 500); 
     builder.setWhen(when); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 
     builder.setContentIntent(intent); 


     Notification notification = builder.build(); 
     notification.ledARGB = 0xFFff0000; 
     notification.flags = Notification.FLAG_SHOW_LIGHTS; 
     notification.ledOnMS = 100; 
     notification.ledOffMS = 100; 
     notificationManager.notify(1, notification); 

     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(15000); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

希望這有助於。

0

FCM有可能你正在使用的消息顯示兩個消息服務

  1. 顯示消息
  2. 數據消息

。您應該使用數據消息而不是顯示消息。當應用關閉時,顯示消息將無法使用。

相關問題