2017-07-21 30 views
8

我試圖推動與NotificationCompat通知:Android的推送通知的旗幟沒有出現在某些設備

NotificationCompat.Builder b = new NotificationCompat.Builder(this); 
      b.setAutoCancel(true) 
        .setDefaults(NotificationCompat.DEFAULT_ALL) 
        .setWhen(System.currentTimeMillis()) 
        .setSmallIcon(this.getResources(). 
         getIdentifier("ic_launcher", "mipmap", this.getPackageName())) 
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
         this.getResources(). 
         getIdentifier("ic_launcher", "mipmap", this.getPackageName()))) 
        .setTicker("God Reacts") 
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
        .setPriority(Notification.PRIORITY_MAX) 
        .setContentTitle(data.get("lineOne")) 
        .setContentText(data.get("lineTwo")) 
        .setContentInfo("Spread the message !"); 

      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
        new Intent(this,getMainActivityClass()), 
        PendingIntent.FLAG_UPDATE_CURRENT); 
      b.setContentIntent(contentIntent); 
      NotificationManager nm = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 
      nm.notify(1, b.build()); 

但在少數設備(三星,MI等)的通知旗幟不顯示。 通知在聲音和振動的動作托盤中滑動。

但是在很少的設備中,當應用程序關閉/背景/前景時,它顯示得很完美。正確彈出的設備使用棉花糖。是由於特定的操作系統?或者它是與設備相關的問題?我需要額外增加什麼?

+0

只是在某些特殊情況下,某些設備可能會安裝通知清理應用程序,它們可以清理您的通知。我之前遇到過這個問題,這完全讓我發瘋。也許你可以檢查它。 –

+0

@ Spark.Bao我不認爲是這種情況。我在2個真實設備上檢查了它,沒有通知清理應用程序。我不明白這些問題發生的原因 –

+0

您是否檢查過這個:https:// stackoverflow .com/questions/29522254/get-android-notification-to-appear-as-banner? – josealfonsomora

回答

0

問題在於某些設備的設置。

需要打開'Floating notifications'從設置 - >應用程序 - >通知。

+0

您可以在解決方案中添加更多詳細信息嗎? – arul

+0

@arul沒有什麼更多。 –

2

由於電池優化,在某些牛軋糖版設備中可能會出現此問題。
例如。三星Galaxy S8和S8 +,萬普拉斯,xaiomi等,但在牛軋糖的Android版本, 你可以試試這個曾經,對於你必須檢查一次設置,

第1步:轉到設置>

步驟2:搜索「電池優化」>

步驟3:從這裏,輕觸「應用程序未優化」並切換到「所有應用程序」。

第4步:搜索您的應用程序(其中你沒有得到通知)

第5步:水龍頭上的程序名稱,並設置爲不是最優化的,使其可以收到通知。

OR

第1步:進入設置>應用

第2步:點擊:菜單的右上角,選擇> [特殊訪問]

一步3:選擇>優化電池使用情況>

第4步:點擊下拉菜單,向屏幕的頂部,上面寫着:「應用程序沒有經過優化[V]」 - >更改爲[所有應用]

第5步:選擇您的應用程序和變化它「未優化」。

+0

謝謝你的答案,但問題仍然存在於舊版本(例如kitkat) –

+1

好吧!會嘗試其他的東西,並更新你的工作。 –

3

在中國設備如MI或OPPO等。系統關閉了非白名單應用程序的通知服務。因此,您無能爲力。簡單地請求他們將您的應用程序列入白名單(通常發生的慘淡機會),或者使用黑客總是以某種方式將您的應用程序保存在內存中,例如運行一些空白後臺服務24 * 7

+0

我的應用確實運行了一些後臺服務 –

1

先嚐試使用最小通知屬性。嘗試下面的代碼在我的應用程序中正常工作。如果它可以通過啓用通知的一個屬性來調試代碼。

private void sendNotification(String messageBody) { 
Intent intent = new Intent(this, NotificationActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_action_notification) 
      .setContentTitle("Touchrooms Notification") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
0

我有類似的問題,當通知使用圖標或圖像,無法加載該圖標/圖像。您可以輕鬆地通過改變負載圖標,從資源的設置圖標的那些行類似下面測試通過res_id

b.setSmallIcon(R.drawable.new_mail); 
b.setLargeIcon(R.drawable.my_icon); 

您當前加載這些圖標上這些設備,因此可能失敗的方式,通知將有聲和振動,但沒有可見的內容會出現。

相關問題