Reputation: 335
I am so ashamed for asking this question because i know better, but right now I am so stuck! I wrote my code to call an activity when a notification is clicked. However, overtime i click the notification, the activity does not start, instead the notification just goes away. I have a Receiver.java which extends BroadcastReceiver and then calls the Alarm Service which contains the notification builder I set up. Here is the notification builder I have used:
Intent intent1=new Intent(Alarm.this,NotificationPage.class);
PendingIntent pendingIntent=PendingIntent.getActivity(Alarm.this,1,intent1,0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(Alarm.this);
builder.setAutoCancel(true);
builder.setContentTitle(from);
builder.setContentText(message);
builder.setSmallIcon(R.drawable.icon);
builder.setContentIntent(pendingIntent);
builder.setSound(sound);
builder.build();
nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notify=builder.getNotification();
notify.flags=Notification.FLAG_AUTO_CANCEL;
nm.notify(0, notify);
everything but the notification page popping up, works. I tested it on a device and the app did not crash so I know there is no error. Please help!
Frustrated coder atm
Upvotes: 0
Views: 79
Reputation: 1074
public void notifyIncomingRequest(Context context,String message)
{
Intent intent=new Intent();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent.putExtra("notification", true);
intent.putExtra("message", message);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setClass(context, YOURCLASS.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true);
boolean notification = true;
boolean notification_vibration = false;
boolean notification_sound = true;
String title = "YOURTITLE";
mBuilder.setContentTitle(title);
String msg=message;
mBuilder.setContentText(msg);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(title));
if(notification){
mBuilder.setContentIntent(contentIntent);
Notification notify = mBuilder.build();
if(notification_vibration)
notify.defaults |= Notification.DEFAULT_VIBRATE;
if(notification_sound)
notify.defaults |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(Integer.valueOf(intent.getIntExtra("number", 13)), notify);
}
}
YOURCLASS.class is the Activity that you want to be displayed after clicking notification, try to call this function with appropriate context and message and you are good to go.
Upvotes: 1