I'm donloading files from the Internet using IntentService and displaying a Notification while download is in progress. After download complete I need to be able to click on the notification to open downloaded file in appropriate application. Here's a code I'm using for this:
Intent intent = IntentUtils.getOpenFileIntent(task.getTargetFolder()
+ File.separator + task.getFileNode().getName());
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent pi = taskStackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
And here's how I create the Intent:
public static Intent getOpenFileIntent(String path) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = fileExt(path);
String type = mime.getMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
return intent;
}
The issue is that tapping on the notification closes any of the currently opened app. I just need to display application chooser over the currently opened app. I think that the issue is in TaskStackBuilder usage but there's no other way to create PendingIntent instance for ACTION_VIEW Intent.
You need to create PendingIntent and set to your Notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_CANCEL_CURRENT)
Fore more information go to http://developer.android.com/guide/topics/ui/notifiers/notifications.html
private NotificationManager manager;
private Intent notiIntent;
/// id is integre value which in unique for notifications
notiIntent= new Intent(context, CurrentActiivty.class);
notiIntent.putExtra("id", id);
notiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification(R.drawable.icon, "your text on notification bar", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notiIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context,"text here", message, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notiIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
manager.notify(id, notification);
Now In calling actiivty write the following code
Bundle extras = getIntent().getExtras();
int id= extras.getInt("id");
NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(id);
Related
I am trying to start an activity from a notification. Upon starting that activity, I add data via intent.putextra to the intent so the activity shows the right content for the situation.
The activity that is being started is supposed to be open only once in the stack. I did achieve this via
android:launchMode="singleTop"
in my manifest.
However - and now I come to my question - if this activity is already running, I want it to close and replace it with the instance I am creating with the specific additional data (put extra). How can I achieve this?
Heres the code of my notification:
public void newMessageNotification(String title, String message, String otherusernumber, String requestStatus, String sendername) {
notificationManager = NotificationManagerCompat.from(this);
Intent chatIntent = new Intent(this,ChatActivity.class);
//these three strings define the behaviour of the chat activtity
chatIntent.putExtra("otherUsername", sendername);
chatIntent.putExtra("otherUsernumber", otherusernumber);
chatIntent.putExtra("requestStatus", requestStatus);
chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), chatIntent,
PendingIntent.FLAG_UPDATE_CURRENT);;
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_new_message)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
Try below code
Add PendingIntent in notification
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Full create notification code
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =
(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager . getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String Ch_id = "yourappname_01";
// The user-visible name of the channel.
CharSequence name = "Notification";
// The user-visible description of the channel.
//String description = getString(R.string.channel_description);
int importance = NotificationManager . IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(Ch_id, name, importance);
mChannel.setSound(defaultSoundUri, new AudioAttributes . Builder ().build());
notificationManager.createNotificationChannel(mChannel);
// Create a notification and set the notification channel.
notification = new Notification . Builder (this, Ch_id)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setContentIntent(pendingIntent) //Add PendingIntent
.setChannelId(Ch_id)
.build();
} else {
// Create a notification
notification = new Notification . Builder (this)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent) //Add PendingIntent
.build();
}
//Generate Diff Notification
int m =(int)((new Date ().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m, notification);
Update
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);
I hope this can help you!
Thank You.
Try to add flag: Intent.FLAG_ACTIVITY_NEW_TASK to your Intent
Try Below
Intent intent = new Intent(this, ActivityDestination.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//pendingIntent = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ACTIVITY_NEW_TASK);
I'm using Google Play Services lib for GCM and all works great.
Now I implement 2 buttons (actions) in my notificationCompat.Builder:
- One open app.
- Other one delete some unread messages: for this operation I post some dato to server.
How can I launch a post to my server on notification action click without open any activity in front of user?
Thank you very much!!!!!
Here my code:`Intent openIntent = new Intent();
Intent cancelIntent = new Intent();
if (has1Id) {
//go details!
openIntent = new Intent(this, NotificationActivity.class);
openIntent.putExtra("id", ids.get(0));
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids.get(0));
}else{
//go home!
openIntent = new Intent(this, HomeActivity.class);
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelNotificationIntent = PendingIntent.getActivity(this, 0,
cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long[] vibrationPattern = {0, 200, 800, 200, 100, 500};
Uri soundUri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.jingle1);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title) //set title
.setContentInfo(numNotRead) //number of unread content
.setAutoCancel(true)
.setContentText(msgs.get(0)+"...")
.setVibrate(vibrationPattern)
.setSound(soundUri)
.setLights(Color.rgb(255, 165, 00), 800, 800)
.setContentIntent(contentIntent).addAction(R.drawable.ic_notification, getString(R.string.open_app), contentIntent)
.setContentIntent(cancelNotificationIntent).addAction(android.R.drawable.ic_delete, getString(R.string.mark_read), cancelNotificationIntent);
if (has1Id) {
NotificationCompat.BigTextStyle bigTextStyle =new NotificationCompat.BigTextStyle().bigText(msgs.get(0));
mBuilder.setContentTitle(getString(R.string.notification_new_content));
//get only 1 long text: the description of content
mBuilder.setStyle(bigTextStyle);
}else{
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(getString(R.string.notification_new_contents));
for (int i=0; i < msgs.size(); i++) {
inboxStyle.addLine(msgs.get(i));
}
mBuilder.setStyle(inboxStyle);
}
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());`
You should use another class as the intent receiver for cancelIntent, like a BroadcastReceiver and handle the intent there with no UI.
public class MyIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Handle a notification
}
}
}
You also can use a Activity with no UI as the intent, but I think previous option is better.
More info:
Android Activity with no GUI
I have an app which has 4 layers/stack, something like,
HomePage >> DetailPage >> MapPage >> ChatPage
I managed to create notifications whenever I received different chat messages (note that each conversation has its unique id, say ChatId)
for different conversation, I will create a notification using ChatId as the requestCode in the pendingIntent
The problem I am facing is that, whenever I received more than 1 notification, say ChatId1 comes first, followed by ChatId2. Then when I clicked on the ChatId1's notification, it will launch the latest intent which is ChatId2, making the conversations loaded in ChatPage are conversations of ChatId2.
Question is, how can I make it so that, when I clicked on ChatId1's notification, it loads messages of ChatId1, and when click on ChatId2, it loads messages of ChatId2.
Below is the snippet of the code where I create my notifications
public void createLocalNotification(String meetingIdLong, String countryCode, String phoneNumber, String contactName, String chatMessage) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, ChatLog.class);
long L = Long.parseLong(meetingIdLong);
int a = Integer.parseInt(meetingIdLong);
intent.putExtra("meetingId", L);
Intent intent1 = new Intent(this, HomePage.class);
Intent intent2= new Intent(this, MeetingDetailPage.class);
intent2.putExtra("meetingId", L);
Intent intent3 = new Intent(this, MapPage.class);
intent3.putExtra("meetingId", L);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ChatLog.class);
stackBuilder.addNextIntent(intent1);
stackBuilder.addNextIntent(intent2);
stackBuilder.addNextIntent(intent3);
stackBuilder.addNextIntent(intent);
PendingIntent pIntent = stackBuilder.getPendingIntent(a, PendingIntent.FLAG_UPDATE_CURRENT);
String notificationSound = _defPrefs.getString("pref_notification_tone", "");
NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
.setContentTitle(contactName)
.setContentText(chatMessage)
.setSmallIcon(R.drawable.noti_smallicon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.noti_smallicon))
.setSound(Uri.parse(notificationSound))
.setDefaults(Notification.DEFAULT_VIBRATE)
.setNumber(++numberMsg)
.setAutoCancel(true)
.setContentIntent(pIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[6];
events [numberMsg - 1] = new String(chatMessage);
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle(contactName);
inboxStyle.setSummaryText(numberMsg + " new message(s)");
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
noti.setStyle(inboxStyle);
String isChatOpen, isChatOpenId;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
isChatOpen = preferences.getString("isChatOpen","");
isChatOpenId = preferences.getString("isChatOpenId","0");
if (!isChatOpenId.equals(meetingIdLong))
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(a, noti.build());
}
}
Use
intent.setAction(Long.toString(System.currentTimeMillis()));
for each your Intent you want to be unique. Its just a dummy action, but it helps.
I am trying to open the browser with a url when the user click on the push notification, i search in stackoverflow and i find this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
but it doesnt work for me, when i put that the notification doesnt appear, i debugged it and it only throw the class file editor no error or anything.
this is the code
public void mostrarNotificacion(Context context, String body,String title, String icon, String url,String superior)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notManager = (NotificationManager) context.getSystemService(ns);
int icono = R.drawable.mydrawable;
CharSequence textoEstado = superior;
long hora = System.currentTimeMillis();
Notification notif = new Notification(icono, textoEstado, hora);
Context contexto = context.getApplicationContext();
CharSequence titulo = title;
CharSequence descripcion = body;
PendingIntent contIntent;
if(url.equalsIgnoreCase("NULL"))
{
Intent notIntent = new Intent(contexto,MainActivity.class);
contIntent = PendingIntent.getActivity(
contexto, 0, notIntent, 0);
}
else
{
// Intent i = new Intent(Intent.ACTION_VIEW);
//i.setData(Uri.parse(url));
// contIntent = PendingIntent.getActivity(contexto, 0, i, 0);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
// notif.setLatestEventInfo(contexto, titulo, descripcion, contIntent);
//AutoCancel:
notif.flags |= Notification.FLAG_AUTO_CANCEL;
//send notif
notManager.notify(1, notif);
}
What you need to do is set a pending intent -
which will be invoked when the user clicks the notification.
(Above you just started an activity...)
Here's a sample code :
private void createNotification(String text, String link){
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(text);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// pending implicit intent to view url
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pending);
// using the same tag and Id causes the new notification to replace an existing one
mNotificationManager.notify(String.valueOf(System.currentTimeMillis()), PUSH, notificationBuilder.build());
}
Edit 1 :
I changed the answer to use PendingIntent.FLAG_UPDATE_CURRENT for the sample purpose. Thanks Aviv Ben Shabat for the comment.
Edit 2 :
Following Alex Zezekalo's comment, note that opening the notification from the lock screen, assuming chrome is used, will fail as explained in the open issue : https://code.google.com/p/chromium/issues/detail?id=455126 -
Chrome will ignore the intent, and you should be able to find in your logcat -
E/cr_document.CLActivity﹕ Ignoring intent: Intent { act=android.intent.action.VIEW dat=http://google.com/... flg=0x1000c000 cmp=com.android.chrome/com.google.android.apps.chrome.Main (has extras) }
I have the following code that creates a notification when an SMS message is received by the phone. It displays the notification correctly; however, when the user clicks the notification, nothing happens. It should open up the SMS inbox so the user can view their message. Thanks in advance.
mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
Uri uri = Uri.parse("content://sms/inbox");
PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0, new Intent(Intent.ACTION_VIEW, uri), Intent.FLAG_ACTIVITY_NEW_TASK);
String tickerText = arg0.getString(R.string.newmsg, msgs[i].getMessageBody().toString());
Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());
notif.setLatestEventInfo(arg0, msgs[i].getOriginatingAddress(), msgs[i].getMessageBody().toString(), contentIntent);
notif.vibrate = new long[] { 100, 250, 100, 500 };
mNotificationManager.notify(R.string.recv_msg_notif_id, notif);
I got it to work. The following code shows how to accomplish this task:
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setType("vnd.android-dir/mms-sms");