Notification page does not come up when notification is clicked - android

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

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.

Related

Android PUSH notifications with PendingIntent overriding previous

I have an application that receive PUSH notifications. When notification "not1" is received, it shows as usual using notification manager. But when "not2" is received, is not showing below "not1" but overriding it. I need show all notifications.
I thought that using different requestCode for PendingIntent will solve the problem, but not. I also tried with PendingIntent.FLAG_UPDATE_CURRENT flag and many others (including no flags). My code right now is like this:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
Notification notification = null;
Intent notificationIntent = new Intent(context.getApplicationContext(), SplashActivity.class);
notificationIntent.putExtra("text", text);
notificationIntent.putExtra("url", url);
PendingIntent intent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = setBuilderNormalNotification(builder, context, text, intent);
notification = new NotificationCompat.BigTextStyle(builder).bigText(text).build();
notificationManager.notify(0, notification);
Where function setBuilderNormalNotification is the next:
private static NotificationCompat.Builder setBuilderNormalNotification(NotificationCompat.Builder builder, Context context, String text, PendingIntent intent)
{
builder
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), getNotificationIcon()))
.setContentText(text)
.setContentIntent(intent)
.setWhen(0)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true);
return builder;
}
What Am I doing wrong, please?
Thanks a lot.
In your code notificationManager.notify(0, notification); That 0 is the constant ID you are using, since it is always the same the notifications stack.

Intent service, choose activity to launch

I have a class that implements IntentService that retrieve some data from a webservice, and if a condition is verified, show notification on notification bar's device.
This is the code:
public BackgroundService() {
super("SERVICENAME");
}
#Override
protected void onHandleIntent(Intent intent) {
if (verifyConditions()) {
showNotification();
}
}
private void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.notification_icon);
// .setContentIntent(pendingIntent);
Intent notificationIntent = new Intent(this.getApplicationContext(),
SplashScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
// set sound
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();
// Hide the notification after it's selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
Now if user tap on notification, SplashScreen activity is launched (it's first activity of my app). this is ok if user has close the application, but if application is in foreground, tapping on notification cause restart of app.
There's a fast way to check if app is in foreground, and if yes, launch a different activity thant SplashScreen?
Hope this will help. You check your app is running foreground or not and according to that change intent for PendingIntent visit StackOverFlow : check android application is in foreground or not?

Notification auto cancel not working for Android lollipop

I want to auto cancel my notification when user clicks on notification. The following code works good in all the devices except Android lollipop device. In Lollipop device, notification goes only when user swipes it off.
#SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
What is missing in this code?
Looks good to me. My auto cancel still works on 5.0.2. Let me give you a portion of my code:
public static void notif(int id, String title, String text, Context context, Class activity) {
// get an instance of the NotificationManager service
if (mNotifyMgr == null)
mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notifIntent);
mNotifyMgr.notify(id, mBuilder.build());
}
modify following:
setAutoCancel(true) -> setAutoCancel(false);
then on action activity do the following
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if you want to set special condition you can set by
intent.putExtra("key", "value")

How to remove notification when click on notification android

Can i remove this notification in a click on notication?
I can cancel notification when click on this notification using the setAutoCancel(true).
but not clear notification after click on notification.
private void sendNotification(String msg, Bundle extras) {
notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent;
if (extras != null) {
intent = new Intent(this, MessageConversationActivity.class);
intent.putExtra("sender_id", extras.get("sender_id").toString());
intent.putExtra("receiver_id", Application.ACTIVE_USER.getUser_id());
intent.putExtra("gender", extras.get("gender").toString());
intent.putExtra("profile_picture", extras.get("profile_picture")
.toString());
intent.putExtra("username", extras.get("username").toString());
} else {
intent = new Intent(this, MainActivity.class);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
String app_name = getString(R.string.app_name);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.app_icon)
.setContentTitle(app_name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
builder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Try This
builder.setAutoCancel(true);
If you set your notification to auto-cancel it is also removed once the user selects it.
OR
You can also call the cancel() for a particular notification ID to cancel notification.
notificationManager.cancel(notifyId);
Use this in the MessageConversationActivity class onCreate method.
Use notificationManager.cancel(NOTIFICATION_ID);
It cancel the notification id from the notification manager
you can do this by using
Notification.FLAG_AUTO_CANCEL
then start intent of page you want

how to launch an activity via notification received by GCM and set text in that activitiy android?

I have developed an app in which I used GCM service to get notification, now when I received notification I want to launch an activity and in that activity I have to set a text received by GCM to a textview.My problem is that the activity which is getting launch by tapping on notification is able to set text only when the app is in foreground but not when the app is in background.
here is the code snippet I used.
#SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// Intent notificationIntent = new
// Intent().setClassName("com.ninehertz.bella",
// "com.ninehertz.bella.BellaNotificationActivity");
Intent notificationIntent = new Intent(context,
BellaNotificationActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// notification.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Try something like this.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
//put your extra message from notification and get from bundes in in onCreate in ResultActivity
resultIntent.putExtra(EXTRA_MESSAGE,message);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
you can try this code.I have no problem whether the app in foreground or background.
private static void generateNotification(Context context, String message) {
//NotificationActivity will be called when tapping notification
Intent notificationIntent = new Intent(context, NotificationActivity.class);
//this message will be carried away to NotificationActivity and you can setetxt
notificationIntent.putExtra("msg",message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noti = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(" New message ")
.setSmallIcon(R.drawable.city)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(intent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
noti.setAutoCancel(true);
notificationManager.notify(001,noti.build());
}

Categories

Resources