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?
Related
Hello I am developing an app in which i want to give push notifications to user when Sms receives. Now the problem is that the notification icon still does not remove when app open by user through launcher icon
Here is my code:
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(title);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_VIBRATE;
//notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
I tried like this too:
notificationManager.cancel(0);
and also tried this:
notificationManager.cancelAll();
but both these doesnot work. They doesnot allow notification to occur. Maybe they are cancelling the push notification before its creation.
Please Help!
Just cancel all the notifications inside the main Activity's onCreate() method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
That is because you asked
Remove notification icon when user open app through launcher Icon
But best way is to put it in onResume() so that when the app is in background you want to show the notification and remove it when user brings it to foreground.
#Override
protected void onResume() {
super.onResume();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
I'm using the following code:
private void startForeground(boolean isActive) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(isActive ? R.string.title_agent_active : R.string.title_agent_inactive))
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
and
#Override
protected void onHandleIntent(Intent intent) {
startForeground(true);
...
With this code notification does not appear.
But if I replace startForeground(...) with NotificationManager.notify(...) — it shows the notification, so, I think, that at least notification building code is ok.
Where could be the problem?
If you're using an IntentService the service will be destroyed as soon as onHandleIntent() method finishes execution and then, the notification will be removed.
try to change the id into the other
start in onCreate() method
check the notifications are not blocked in the device
try to use these flags: noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Use usual Service class
try this solution (works in my app):
private void fg() {
Intent intent = launchIntent(this);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent);
noti = mBuilder.build();
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
startForeground(_.ID, noti);
}
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.
I'm having trouble in figuring out the Intent, PendingIntent Filters and Flags for notifications.
Notification are working and are getting generated as they should, but the problem is only the last notification created keeps the Bundle Data.
I want all notification to keep the Bundle Data of each notification in it until a user click on them.
Consider an application for a moment when different user send you a message a new notification gets created and when you click on any notification the app launches and takes you to some specific Activity. I want the same thing but when there are more than one notification the last notification keeps the Data where as previous notification loose their Bundle Data and Intent.
There is another thing which Filters to use to restrict the app from launching a new instance of MainActivity everytime a notification is clicked at.
The Notification_ID are different for each notification.
public class AlarmSchedulingService extends IntentService {
private NotificationManager mNotificationManager;
public AlarmSchedulingService() {
super("SchedulingService");
}
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
sendNotification(extras.getInt(KEY_EXTRAS_NOTIFICATION_ID));
}
public void sendNotification(int NOTIFICATION_ID) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(keyName, extraData);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// use the right class it should be called from the where alarms are set
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(titleString)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(messageString))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setContentText(messageString);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
This is Showing that :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
You are giving request code 0 to all notifications.
0 should be replaced by each unique number, otherwise, each new notification will override the old one.
I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).