我有一个显示通知的服务,该通知适用于Android 6.0及之前的版本但不会显示在7.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 :(得分:0)
请按照以下步骤操作,让我知道它是否有效 从电池优化中删除6ya:
1.转到设置电池。
2.单击电池菜单,选择电池优化。
3.点击未优化并转到所有应用。
4.在所有应用程序中找到6ya应用程序并单击它。
5.它将显示一个包含Optimize和Do not optimize的弹出窗口。
6.点击“不优化”并点击“完成”。
7.6ya应位于未优化的文件夹中。
8.All set - 重启手机。
答案 1 :(得分:0)
我担心我无法重现您所看到的问题。
在我的测试中,此代码在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);
}
}