2017-07-19 58 views
0

我正在嘗試進行平視通知工作。通知已創建,但未顯示在應用程序的頂部。 這裏是負責建設的通知代碼:Android單挑通知沒有顯示

Notification notification = new NotificationCompat.Builder(context) 
           .setSmallIcon(android.R.drawable.arrow_up_float) 
           .setContentTitle("Check running time - click!") 
           .setContentText(String.valueOf(elapsedTime)) 
           .setContentIntent(pendingIntent) 
           .setDefaults(Notification.DEFAULT_ALL) 
           .setPriority(Notification.PRIORITY_HIGH) 
           .setVibrate(new long[0]) 
           .build(); 

,我試圖運行應用程序的設備API 21.我見過很多線程,但沒有辦法給我的作品。

回答

0

嘗試做到這一點:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) 
           .setSmallIcon(android.R.drawable.arrow_up_float) 
           .setContentTitle("Check running time - click!") 
           .setContentText(String.valueOf(elapsedTime)) 
           .setContentIntent(pendingIntent) 
           .setDefaults(Notification.DEFAULT_ALL) 
           .setPriority(Notification.PRIORITY_HIGH) 
           .setVibrate(new long[0]); 

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

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

它不工作,我會驚訝,如果它。 –

+0

你可以添加所有的類代碼嗎? –

-1

只需使用像

NotificationManager mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent myintent = new Intent(this, MainActivity.class); 
    myintent.putExtra("message", msg); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      myintent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("ttile") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(1, mBuilder.build()); 
+0

它通過調用setStyle()會有幫助嗎?還是行不通。 –

+0

請務必導入導入android.support.v7.app.NotificationCompat; –

+0

或者您可能會更新22.2.0,22.2.1 –

0


你的代碼幾乎是罰款。我使用DEFAULT_VIBRATE代替DEFAULT_ALL的:

builder.setPriority(Notification.PRIORITY_HIGH); 
if (Build.VERSION.SDK_INT >= 21) { 
    mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); 
} 

但是,你還可以設置

builder.fullScreenIntent(sameAsContentPendingtIntent); 

但^這個人是不確定性。我的意思是系統選擇顯示HeadsUp或啓動意圖。在大多數情況下,它會顯示HeadUp,但由於Android版本,製造商,啓動程序等原因,我並不指望它。
最後,您還需要定義正確的通知通道。我認爲你已經這樣做了,因爲如果你沒有看到任何通知:-)哦,可愛的Android :-)無論如何,我想說NotificationChannel也需要高優先級:

channel = new NotificationChannel("uniqueId", "name", NotificationManager.IMPORTANCE_HIGH); 
channel.enableVibration(true); 

另外,我建議你,查看developer.android.com上的最新意見
谷歌將從現在開始變得更加嚴格。不僅是通知,而且'targetApi',但這是另一回事,請原諒我:-)
今天有一個很好的代碼