it's very weird and it happens just for my app. When I turn off my phone screen and turn it on again, it clears all of my notification despite it doesn't happens for other apps on my phone.
this is my code:
Intent resultIntent = resultIntent = new Intent(mContext, intentClass);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.putExtra("onvan", title);
resultIntent.putExtra("message", message);
resultIntent.putExtra("link", link);
resultIntent.putExtra("linktype", linktype);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder TSB = TaskStackBuilder.create(mContext);
TSB.addParentStack(intentClass);
TSB.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
new Random().nextInt(),
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
I don't want to clear the notification when the screen turns off, I want it to clear when use manually clears it
what is wrong with my code?
You're using
setAutoCancel(true)
As per documentation
setAutoCancel(boolean autoCancel)
Make this notification automatically
dismissed when the user touches it.
I would try setting auto cancel to false.
Related
I'm new to android. And to show notification on android phones I got this below code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /*Request code*/ , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mytune_default)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m /*ID of notification*/ , notificationBuilder.build());
But the thing is I just want that notification unread count on code appview on system, but dont want other things loke message, title etc that appear on notification tray of system. How come this be acheived>
It's not possible to replace launcher icon with widget. You can say to user to place your widget on the home screen, but not in application list. Users must do that in their own. Nevertheless, it's iOs pattern. It's not good idea to bring iOS design elements to android. Android have great notification bar, where user expect to see notification from your app. Android users don't expect to see it over the icon.
I have searched a lot over the internet and could'n find an answer.
I am working on a launcher project and on a click of a button, I want to launch default notification bar . Is there any API.
you can implement the android default notification using the below code. you can customize.
// Invoking the default notification service
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
mBuilder.setContentTitle("ur titlw");
mBuilder.setContentText("ur content");
mBuilder.setTicker("ur sticker");
mBuilder.setSmallIcon(R.drawable.uricon);
// Increase notification number every time a new notification arrives
mBuilder.setNumber(++numMessagesOne);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ur activity where this notification shout appear);
resultIntent.putExtra("notificationId", "test");
// This ensures that navigating backward from the Activity leads out of
// the app to Home page
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent
stackBuilder.addParentStack(ur activity where it returns when click on the notification);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_ONE_SHOT // can only be used once
);
// start the activity when the user clicks the notification text
mBuilder.setContentIntent(resultPendingIntent);
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// pass the Notification object to the system
myNotificationManager.notify(notificationIdOne, mBuilder.build());
I hope this will be useful.
I've read multiple questions and answers surrounding this issue, however I cannot get any of them to work for me.
I have a notification that on click I would like to bring the application to the front and resume rather than close and restart.
This is my notification code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("example")
.setContentText("example");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// 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(MainActivity.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);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
And in my manifest file I have
android:launchMode="singleTop"
Can anyone see what is going wrong? I get no errors, although the notification refuses to resume the app and instead restarts it.
Fixed by using this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sound Asleep")
.setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 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.
// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
In Manifest
android:launchMode="singleTop"
I had some similar issue recently change your intent flags with this flags:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Edit: and remove that launchmode from your manifest.
hope it help
Use below code, may it will help
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sound Asleep")
.setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 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.
// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
Pretty new in android here :)
I have a notification builder which works with no problem if the application target version is > 4.0
However when I switch to 2.3 I get an error on this line which says "Notificaiton.Builder cannot be resolved to a type".
Notification notification = new Notification.Builder(this)
.setSmallIcon(drawable_small)
.setLargeIcon(drawable_big)
.setWhen(System.currentTimeMillis()).setTicker(content_title)
.setContentTitle(content_title).setContentInfo(content_info)
.setContentText(content_text).setContentIntent(pIntent)
.getNotification();
This problem is now solved!
However I`m having another one now
It gives me an error on every R (resource) and I have the option to Import R.
If I import it,its giving me errors on every resource.
setContentView(R.layout.activity_main);
Implement your Notification like
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(icon_small)
.setTicker(message)
.setLargeIcon(largeIcon)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(message)
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
And add support library v4 into your project and also import
import android.support.v4.app.NotificationCompat;
NotificationCompat helper for accessing features in Notification introduced after API level 4 in a backwards compatible fashion.
For more information go to:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Try this :
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message,
contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
Hope this helps.
Add the v4 support library to your project by right-clicking your project, then choosing Android Tools > Add support library
Change to NotificationCompat.Builder
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher,
"New Message",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Got new Message";
String notificationText = "";
Intent myIntent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent pendingIntent
= PendingIntent.getActivity(MainActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
n.defaults |= Notification.DEFAULT_SOUND;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(1,n);
Hope this works out:)
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);
// 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(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.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());
And add support library v4 into your project and also import
import android.support.v4.app.NotificationCompat;
I have a simple app that shows android notifications depending to one of three buttons you press.
Dog, Cat, or Mouse
If you press dog then a notification that says "Dog" shows.
If you press cat then a notification that says "Cat" shows.
If you press mouse then a notification that says "Mouse" shows.
When you click the notification it just takes me to my main activity (with the three buttons again). I would like it to take me to a separate activity, that will read what notification was clicked, and set it the text from the notification as a textView.
Any ideas on how to do 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);
// 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());
Source:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Actions