通知没有显示Android 7.0,但显示在Android 6.0

问题描述:

我有一个显示通知的服务,通知在Android 6.0和之前版本中可用,但它不会在7.0中显示。通知没有显示Android 7.0,但显示在Android 6.0

相关代码:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    PendingIntent pendingIntentMain = PendingIntent.getActivity(this, 0, intent_main, PendingIntent.FLAG_CANCEL_CURRENT); 
    Notification notificationPopup = new Notification.Builder(this).setContentTitle("Alarm is ON!").setContentText("Click here") 
      .setContentIntent(pendingIntentMain).setAutoCancel(true).setSmallIcon(R.drawable.acd).setPriority(Notification.PRIORITY_MAX) 
      .setDefaults(Notification.DEFAULT_ALL).build(); 
    notificationManager.notify(0, notificationPopup); 
+0

你可以发表你的manfest.xml吗? – Jeeva

请跟进这个步骤,让我知道它的工作原理或不从电池优化 删除6YA:

1.Go来设置电池。

2.单击电池页面上的菜单,然后选择电池优化。

3.点击未优化,并转到所有应用程序。

4.在所有应用程序找到6ya应用程序,然后点击它。

5.它会显示弹出与优化和不优化。

6.单击不优化并点击完成。

7.6ya应位于未优化的文件夹中。

8.全部设置 - 重启手机。

恐怕我无法再现您所看到的问题。

在我的测试,这段代码创建并成功地显示在通知上的是Android 5.1,6.0,7.0和7.1.1:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startService(new Intent(this, MyService.class)); 

    } 
} 

MyService.java

public class MyService extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // create and display a notification 
     Intent intent_main = new Intent(this, MainActivity.class); 
     PendingIntent pendingIntentMain = PendingIntent.getActivity(this, 0, intent_main, PendingIntent.FLAG_CANCEL_CURRENT); 
     Notification notificationPopup = new Notification.Builder(this) 
       .setContentTitle("Alarm is ON!") 
       .setContentText("Click here") 
       .setContentIntent(pendingIntentMain) 
       .setAutoCancel(true) 
       .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) 
       .setPriority(Notification.PRIORITY_MAX) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .build(); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationPopup); 

     return super.onStartCommand(intent, flags, startId); 
    } 
}