2017-01-27 21 views
1

嘿所有我有一個棘手的問題,需要諮詢。在收到FCM數據有效載荷後,我已經手動構建了一個通知。這是怎樣的通知被無論是在前臺和後臺自數據淨荷創建:android通知 - 如何不顯示在前景

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 


    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 

     String msg = remoteMessage.getData().get("message"); 
     sendNotification(msg); 
    } 

private PendingIntent createIntent(String msg) { 
    Intent intent = new Intent(this, SportsActivity.class); 
    Bundle b = new Bundle(); 
     b.putString(Constants.KEY_GO_TO_TAB, Constants.KEY_DASHBOARD_HOCKEY_SCORE_TAB); 
    intent.putExtras(b); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 
    return pendingIntent; 
} 

private void sendNotification(String messageBody) { 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.notification_icon) 
      .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(messageBody)) 
      .setContentText(messageBody) 
      .setColor(ContextCompat.getColor(this, R.color.hockey_brand)) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(createIntent(messageBody)); 

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

    notificationManager.notify(0, notificationBuilder.build()); 
} 

}

它似乎運作良好。即時通訊的問題是我想通知不顯示在應用程序在前臺。沒有掃描活動任務沒有辦法做到這一點?因爲這需要許可

我讀過this article但是這個如何知道你已經從前景走到了背景。此外,android.permission.GET_TASKS已棄用,REAL_GET_TASKS權限也不適用於第三方。我只是想在任何時候知道用戶是在前臺還是在後臺,所以我知道我是否應該顯示通知。我想知道firebase本身是否有一些東西。如果應用程序不在前臺,則從Firebase控制檯發送「通知有效內容」時,不會在通知面板中顯示,因此應該有辦法執行此操作。

+0

也許[本SO後(http://stackoverflow.com/a/39589766/5015207)是有幫助嗎? – 0X0nosugar

+0

即使android.permission.GET_TASKS是我相信的棄用權限,謝謝。告訴我,如果我錯了。我甚至不會在列表中看到它。和即時支持api 19,所以我不能使用getAppTasks();你知道如果android.permission.GET_TASKS是一個危險的許可? – j2emanue

+0

Juozas Kontvainis的代碼片段似乎不需要任何許可。我想這是因爲它只是試圖獲得關於它自己的應用程序的信息。 – 0X0nosugar

回答