I have a piece of code on my android studio project:
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
// .setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("New updates from StoriesCity!")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
It's working fine and i see the notification, but when i click on it it does not go away..
Maybe it's because i'm using .notify ?
Please help me modify the code to makeit disapeair on click.
I'm a beginner :)
Use:
.setAutoCancel(true);
setAutoCancel
Set flag FLAG_CANCEL_CURRENT instead of 0
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
Also
.setAoutoCancel(true)
Related
I'm following the Android Dev tutorial to use Big View for notifications. I want an on going notification with some functionality when clicking on the body of the notification and other functionality when clicking the Button.
This is my code:
Intent bodyIntent = new Intent(context, MainActivity.class);
innerIntent.putExtra(NOTIFICATION_BODY, "Click on notification body");
PendingIntent bodyPendingIntent = PendingIntent.getActivity(context, 0, bodyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent buttonIntent = new Intent(context, MainActivity.class);
buttonIntent.setAction(NOTIFICATION_BUTTON);
PendingIntent buttonPendingIntent = PendingIntent.getService(context, 0, buttonIntent, 0);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setLights(getResources().getColor(R.color.primary), 50, 10000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0, 50})
.setOngoing(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hello World BIG!"))
.addAction(R.mipmap.ic_image, "Button", buttonPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Then in onResume:
Intent intent = getIntent();
Log.e("m", String.valueOf(intent.getIntExtra(NOTIFICATION_RESULT, 0)));
Log.e("m", String.valueOf(intent.getAction()));
Result:
When clicking on the body of the notification the bodyIntent fires and I get the correct log printing.
When clicking on the button: Nothing happens and MainActivity not even starting.
Thanks,
You should call PendingIntent.getActivity() method if you want to start an activity, but you are creating buttonPendingIntent by calling PendingIntent.getService().
I have a problem when I try click my notification in versiĆ³n 23.0.1, (I updated latest version) and my notification not is called, I tried some methods but not work.
What I do wrong ?
long mId = System.currentTimeMillis();
Intent intent = new Intent(this, FragmentNotif.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
/*intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);*/
/*intent.putExtra("test","test1");*/
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent , PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)mId , intent , PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
//pendingIntent.FLAG_CANCEL_CURRENT;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(mId)
.setLargeIcon(notificationLargeIconBitmap)//(R.drawable.icona_notificacio)
.setSmallIcon(getNotificationIcon())
.setContentTitle("XX XX")
.setContentText(data.getStringExtra("message"))
.setContentIntent(pendingIntent);
Note my class extends from : GCMBaseIntentService
you try change to source code
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent = PendingIntent.getActivity(
your main.this, (int)mId, new Intent(your main.this, your NotificationMessage.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification;
notification = new Notification.Builder(.this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(android.R.drawable.ic_input_add)
.setLargeIcon(bitmap).setWhen(System.currentTimeMillis())
.setSound(Uri.parse(
Environment.getExternalStorageDirectory().getPath()+"/.mp3"))
.build();
2. NotificationCompat.Builder
Notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(text)
.setTicker("Notification")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
Please refer to http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
If i have an activity that closed by a user (user pressed on home so the app is still in app stack)
and then he gets a notification, when he presses on it i start an activity
and now the same activity is opened twice.
How can i prevent this from happening?
My CODE:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setLights(GCMSIntentService.PURPLE, 500, 500)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
android:launchMode="singleTask"
in manifest Or,
use it as flag for your intent.
You need to add FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP,FLAG_ACTIVITY_NEW_TASK flags to your notification intent.So change
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
to
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);
by android:launchMode="singleTask", if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance and adding flags to the intent
FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP
refer task and back stack
Here's the error.
notify functions id has to be changed everytime. You're giving it 1 everytime. thats the error.
mNotificationManager.notify(1, mBuilder.build());
give a different id everytime. it will work like a charm.
For example, use sharedPrefences to provide different id's everytime.
check this,
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setAutoCancel(true)
.setLights(GCMSIntentService.PURPLE, 500, 500)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
SharedPreferences sp = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
myIntValue = sp.getInt("notificationid", 0);
mNotificationManager.notify(1, mBuilder.build());
SharedPreferences sp1 = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp1.edit();
editor.putInt("notificationid", myIntValue+1);
editor.apply();
My question is that how to merge multiple notification?
Here is my code:
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, DemoActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(result))
.setContentText(result);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(++NOTIFICATION_ID, mBuilder.build());
}
It gives me every individual notification like windows phone but I can't merge into a notification. So, please give me advice what to implement in this method.
Below is my code for showing a Notification, and after clicking it I redirect to another activity using the PendingIntent
But when I click on this notification my application hangs;
On checking my LogCat I see continues GC invoke logs
Please correct where am i going wrong:
private void showNotification(int notifyId, String alertMsg, PendingIntent intent) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(alertMsg))
.setSound(soundUri)
.setContentText(alertMsg);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(intent);
mNotificationManager.notify(notifyId, mBuilder.build());
}
ALSO ABOVE CODE DOES PLAYS THE DEFAULT NOTIFICATION SOUND.
Please help.
Thanks in advance.
HERE IS MY PendingIntent Code:
Intent contentIntent = new Intent(this, AccountDetailsActivity.class);
contentIntent.putExtra(EXTRA_TYPE, extras.getString("cat"));
contentIntent.putExtra(EXTRA_ACCOUNT_ID, extras.getString("rem"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFY_REQUEST_CODE, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotification(NOTIFICATION_TYPE_REMINDER, alertMsg, pendingIntent);
THANKS