我想在我的应用中实现通知。我的代码在下面,并且工作正常,它会显示通知,但每当通知到达应用程序崩溃时。我不知道为什么会这样?
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
Intent notificationIntent;
notificationIntent = new Intent(context, Login.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification noti = new NotificationCompat.Builder(context)
.setTicker("ticker message")
.setLargeIcon(largeIcon)
.setWhen(System.currentTimeMillis())
.setContentTitle("title")
.setContentText("message")
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
//noti.number=++count;
//noti.when=System.currentTimeMillis();
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
noti.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults|=Notification.DEFAULT_LIGHTS;
// Vibrate if vibrate is enabled
noti.defaults |= Notification.DEFAULT_VIBRATE;
//Show the notification
notificationManager.notify(notifyID, noti);
请有人帮助我!
答案 0 :(得分:3)
我不确定这一点,但在VIBRATE
l中添加manifest.xm
权限并尝试
<uses-permission android:name="android.permission.VIBRATE" />
并就此给我反馈。
答案 1 :(得分:1)
只需做以下事情,
Intent notificationIntent;
notificationIntent = new Intent(context, Login.class);
//add setFlags
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification noti = new NotificationCompat.Builder(context)
.setTicker("ticker message")
.setLargeIcon(largeIcon)
.setWhen(System.currentTimeMillis())
.setContentTitle("title")
.setContentText("message")
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
noti.defaults|=Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, noti);
同时在androidmanifest.xml中授予权限:
<uses-permission android:name="android.permission.VIBRATE" />