当我的警报管理器触发我的onReceive()方法时,我试图弹出一个通知。这就是我所做的
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire(10000);
startNotification(context);
wl.release();
}
public void setAlarm(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(Activity, "MainActivity.class");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
assert am != null;
am.set(AlarmManager.RTC_WAKEUP, 60000, pi);
}
private void startNotification(Context context){
// Sets an ID for the notification
int mNotificationId = 001;
NotificationManager notificationManager;
NotificationCompat.Builder mBuilder;
// Build Notification , setOngoing keeps the notification always in status bar
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("RandomTitle")
.setContentText("RandomText")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("extra","Extra Notificacion");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);
// context.startActivity(notificationIntent);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Android Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
// Builds the notification and issues it.
notificationManager.notify(mNotificationId, mBuilder.build());
}
我真的很困惑为什么这个通知没有显示,我已经测试了我的警报,它触发后1分钟的beign创建,但通知仍然没有显示。
有什么想法吗?
谢谢
发布于 2018-09-01 06:47:49
来自Android developer:
当您以Android8.0(API26级)为目标时,您必须实现一个或多个通知通道。如果你的targetSdkVersion设置为25或更低,当你的应用程序在Android8.0(API26级)或更高版本上运行时,它的行为与在运行Android7.1(API25级)或更低版本的设备上相同。
因为您的targetSdkVersion
是28,所以还必须在Builder
构造函数中添加channelId
。
将您的代码更改为:
// Build Notification , setOngoing keeps the notification always in status bar
mBuilder = new NotificationCompat.Builder(context, "notify_001") // Add channel ID to the constructor.
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("RandomTitle")
.setContentText("RandomText")
.setOngoing(true);
发布于 2018-09-01 06:21:45
根据Android开发人员文档:
NotificationCompat.Builder NotificationCompat.Builder (上下文上下文)此构造函数在API级别26.1.0中已弃用。请改用NotificationCompat.Builder(Context,String)。所有发布的通知都必须指定NotificationChannel Id。
参考here
编辑:尝试以下操作:
NotificationCompat.Builder
保持原样(使用代表NotificationChannel
的字符串)。NotificationManager
,如下所示:context notificationManager =NotificationManagerCompat.from(上下文);notificationManager.notify(mNotificationId,context
https://stackoverflow.com/questions/52122889
复制相似问题