Notification throwing intents without it's extras - android

I am trying to launch an intent from a notification, but for a reason I don't know the extra is not always sent with the intent.
This is where I create the intent :
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
mContext.getString(R.string.text), 0);
Intent intents = new Intent(mContext, DataAppTabs.class);
intents.putExtra(DataAppTabs.REQUEST, DataAppTabs.REQUEST_DMTAB);
intents.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(mContext.getApplicationContext(), DataAppTabs.REQUEST_DMTAB, intents, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, "Info", mContext.getString(R.string.enginedmnotification_rebootneeded), launchIntent);
notification.flags = Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(DataAppTabs.REQUEST_DMTAB, notification);
In the DataAppTabs class I overrided onCreate() and onNewIntent(). When the notification appears, if I click on it it opens the correct Activity (DataAppTabs, which is a TabActivity), it triggers onCreate or onNewIntent if the activity was already started, but the extra "REQUEST" is not always set (getExtras() is sometimes null).
I have found the same question here with answers telling me I had to use the "FLAG_ACTIVITY_SINGLE_TOP" flag, but it still doesn't work all the time and I am not able to reproduce the problem every time.
Is it something I did wrong ?
Thanks

// Displaying Notification
NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "MyNotification", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Sample.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Notification Title", "Notification Data", contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
int NOTIFICATION_ID=(int)System.currentTimeMillis();
manager.notify(NOTIFICATION_ID, notification);
// Sample.class is the activity to which you want to go when you click on Notification

Related

How to open app in notification in android?

In my app, when completing a task in async file, it shows a notification with this code
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, new Intent(), 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
The problem is when I click the notification, nothing happens. Basically I want it so that, if the app is already open and the user clicks the notification, then nothing should open. If the app is not open (which means its still running but minimized) then I want it to open the app, like maximize it.
Does anyone know how to do this?
Thanks
You need an Intent, sample code:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, MainActivity.class);
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;
notificationManager.notify(0, notification);
This will open your MainActivity

How can I click missed calls icon to open system Missed Calls UI?

I can use the following code to display a notification icon of missed calls, I hope to click the icon to open system Missed Calls UI, how can I do ? Thanks!
At present, I can open ui.CallerMain.class UI if I remove the comment.
BTW, in system Missed Calls UI, missed calls are listed in there.
private void ShowMissCallNotification(Context myContext,String myContentText) {
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.sym_call_missed,
myContext.getString(R.string.app_name),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
CharSequence contentTitle= "Title";
CharSequence contentText =myContentText;
//Intent notificationIntent = new Intent(myContext, ui.CallerMain.class);
//PendingIntent contentItent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);
//notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent);
notificationManager.notify(NotificationID, notification);
}
Set a Pending Intent to the notification which will trigger the Call History.
First create an intent with Call Log
Intent resultIntent = new Intent();
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setType(CallLog.Calls.CONTENT_TYPE);
Then obtain the PendingIntent
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Then set the PendingIntent to your notification builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon()
.setContentTitle()
.setContentText()
.setContentIntent(resultPendingIntent);
notificationManager.notify(id, builder.build());
Now clicking the notification will open the Call Log.
Update: The code commented out in your snippet will work if you create the Intent as mentioned above in this answer. But please be aware that the method by which you are creating notification has been deprecated. Use NotificationCompat class from the support library in future.

values for the intent remain the updated one when making multiple notification for push notification

I am making an app which requires me to send multiple push notifications to users which has distinct values which will be shows when user click on them from status bar.
I am making the intent using the below code
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.when = when;
notification.icon = icon;
Intent notificationIntent = new Intent(context,RecentStoryPage.class);
notificationIntent.putExtra("id", id_leader);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("message", message);
// 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, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, "", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(Integer.parseInt(id), notification);
When I send single notification, this does not make any issues however whenever I make multiple notifications, whichever notification I click on, it always opens with the recent value.
I got my mistake, I made this line error full
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
which I should have made to
PendingIntent intent = PendingIntent.getActivity(context, Integer.parseInt(id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

how to show only one notification at a time for android application

I use the following code to show the notifications
private void SendNotification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");
//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}
Each time a new notification comes, the previous one should be removed and the new one should be shown.
Any clues will be helpful.
Thanx in advance
o.
You are generatting a notification with different id each time (System.currentTimeMillis()), change it for a single ID.
notificationManager.notify(0, notification);
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
NotificationManager.html
There is one way that you should clear all Notification of your Application before you post a newer one.
Give it a shot like,
notificationManager.cancelAll();
To clear a particular Notification you can do it like,
notificationManager.cancel(notification_id);

when to clear notification count of status notification in android

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).

Categories

Resources