Reputation:
I want to implement Notification in my app. My code is below and working fine it's display a notification but whenever a Notification arives app is crashing. I dont know why this happened?
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);
Please some one help me!
Upvotes: 0
Views: 106
Reputation: 889
Just do following things ,
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);
Also give the permission in androidmanifest.xml :
<uses-permission android:name="android.permission.VIBRATE" />
Upvotes: 1
Reputation: 47817
I am not sure about this but add VIBRATE
permission in your manifest.xm
l and try
<uses-permission android:name="android.permission.VIBRATE" />
And give me feedback on this.
Upvotes: 3