2017-07-19 57 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',但这是另一回事,请原谅我:-)
今天有一个很好的代码