I am developing an android app using phonegap. I want to launch the app from the notification bar. Though this may be a duplicate question, but nothing seems to be working for me;few links that i have tried.
http://pilhuhn.blogspot.in/2010/12/pitfall-in-pendingintent-with-solution.html
Open android app from PUSH notification
re-open background application via notification item
stand alone app developed from this links are working but when i integrate with my real project tapping on the notification bar does nothing, below is my code
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, MyClass.class);
intent.putExtra("message", message);
intent.putExtra("shortMsg", shortMsg);
intent.putExtra("source", source);
intent.putExtra("phone", phone);
intent.putExtra("datetime", datetime);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
notificationManager.notify(0, notification);
MyClass.class Code,
Bundle extras = getIntent().getExtras();
if (extras != null)
{
System.out.println("in extras");
//Retrive data from the intent and store them in instance variables
this.message = extras.getString("message");
this.shortMsg = extras.getString("shortMsg");
this.source = extras.getString("source");
this.phone = extras.getString("phone");
this.datetime = extras.getString("datetime");
}
Thanks in Advance,
Nanashi
I use NotificationCompat.Builder because it is much easier to implement.
Intent intent = new Intent(context, Main.class);
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
mNotification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setContentIntent(pintent)
.setSmallIcon(R.drawable.ic_notification)
.setWhen(System.currentTimeMillis())
.setAutoCancel(cancellable ? true : false)
.setOngoing(cancellable ? false : true)
.build();
notificationManager.notify(0, mNotification);
Use setContentIntent and pass the pending intent as an argument.
Intent intent = new Intent(context, MyClass.class);
intent.putExtra("message", message);
intent.putExtra("shortMsg", shortMsg);
intent.putExtra("source", source);
intent.putExtra("phone", phone);
intent.putExtra("datetime", datetime);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
mNotification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setContentIntent(pintent)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pintent)
.build();
notificationManager.notify(0, mNotification);
Related
i have set up my own push notification server to send notification to android devices.
I have handled the notification so on click it always open my custom activity whatever the app was in background or foreground.
The working part : When sending two separated notifications that means when the first notification is received i click on it so the activity is launched that's ok.
I re-send another notification so also when i click on it all goes well and the activity is relauched.Perfect!
The NOT working part : When sending two successive notifications the problem occurs.
When two notifications are received ..i open the first one the activity is launched but when i click on the second one nothing happens !!.
So i think it might be a solution in changing the Intent FLAG or Pending Intent.
I have searched for solutions but all were about handling the notification when app is in foreground or background which is not in my case.
This is my working code:
Intent i = new Intent(this, News_description.class);
i.putExtra("title", title);
i.putExtra("message", message);
i.putExtra("image", image);
i.putExtra("time", time);
i.putExtra("date", date);
i.putExtra("click_action", click_action);
i.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,PendingIntent.FLAG_ONE_SHOT
);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{status, status})
.setContentTitle(getString(R.string.app_name))
.setContentText(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setSmallIcon(R.mipmap.ahed_me)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int id = (int) System.currentTimeMillis();
manager.notify(id, builder.build());
} catch (Exception e) {
e.printStackTrace();
}
I can provide any further information.
I have the Flag FLAG_ACTIVITY_CLEAR_TASK and it works on my app.
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Here my full code, it works perfect for me:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent =
PendingIntent.getActivity(getApplicationContext(), 0,
i, 0);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.alarm)
.setAutoCancel(true)
.setContentTitle(title)
.setPriority(Notification.PRIORITY_MAX)
.setVibrate(vibrate)
.setLights(Color.BLUE, 3000, 1500)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(contentIntent)
.setContentText(body);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int Unique_Integer_Numbe = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
mNotificationManager.notify(Unique_Integer_Numbe, mBuilder.build());
UPDATED:
public SomePushNotificationClass {
private static int NOTIFICATION_ID = 1;
String previousMessageID = "null";
#Override
public void onReceive(Bundle data) {
String messageID = data.getString("message_id");
if (!messageID.equals(previousMessageID) && !messageID.isEmpty()) {
previousMessageID = messageID;
NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent it = new Intent(this, SomeActivity.class);
it.putExtra("key_example_1", someValue);
it.putExtra("key_example_2", someValue2);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.some_drawable);
notificationBuilder.setLargeIcon(someIcon);
notificationBuilder.setContentTitle(someTitle);
notificationBuilder.setStyle(new
// Optional
NotificationCompat.BigTextStyle().bigText(someText));
notificationBuilder.setContentText(someText);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID,
notificationBuilder.build());
NOTIFICATION_ID++;
}
}
}
This is what you need:
Intent it = new Intent(this, SomeActivity.class);
it.putExtra("key_example_1", someValue);
it.putExtra("key_example_2", someValue2);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
I have implemented android FCM in my application , my App only loads a web application in the WebView.
I wanna change the url of webview after receiving (and click) of FCM notification.
I manage the notification in "MyFirebaseMessagingService.java", specifically in sendNotification method.
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, Principal.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
/*HERE I WANNA ACCES, CREATE OR EDIT A WEBVIEW*/
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Formación Alcalá")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setContentText(messageBody);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
I think this should be possible. Please , someone could guide me ?
P.D. Sorry for my bad english.
check this
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("URl", Your URL);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notification Intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);
and in your main Activity :
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("NotificationMessage"))
{
setContentView(R.layout.viewmain);
// extract the extra-data in the Notification
String url= extras.getString("URl");
webview.loadurl(url);
}
}
edit: My code worked fine, I was simply giving it the wrong Activity class (multiple APK build project using shared library).
I'm using the following code to display a foreground notification for my Service. For some reason though, it doesn't show me my activity when I click on the notification.
I've checked out various StackOverflow posts and followed accordingly, and even the sample FakePlayer from CommonsWare but to no avail.
protected void updateNotification(String subtitle) {
// The PendingIntent to launch our activity if the user selects this notification
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Show notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_headset_mic)
.setWhen(System.currentTimeMillis())
.setContentTitle("AirWaves: " + G.SETTINGS.deviceName)
.setContentText(subtitle)
.setContentIntent(pendingIntent)
.setOngoing(true);
startForeground(NOTIFICATION_ID, builder.build());
}
Anyone have an idea where I might be going wrong with this?
i have also implement notification consider the following example
public void sendNotification(Context context,String message, String action) {
try {
int icon = R.drawable.notilogoboss;
String title = context.getString(R.string.app_name);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("message", message);
intent.putExtra("action", action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logoboss))
.setSmallIcon(icon)
.setContentTitle(title)
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int notId = UUID.randomUUID().hashCode();
notificationManager.notify(notId, notificationBuilder.build());
} catch (Exception e) {
}
}
Please try the following method to create the pending intent:
Intent intent = new Intent( context, MainActivity.class );
intent.putExtra( "message", message );
intent.putExtra("action", action);
TaskStackBuilder stackBuilder = TaskStackBuilder.create( context );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( intent );
PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
notificationBuilder.setContentIntent( pendingIntent );
Try changing the flag for the pending intent to this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Also, what is the startForeground(NOTIFICATION_ID, builder.build()); method that you are using? can you post that as well for additional help?
I have prepared a simple test app which posts a notification on a button click:
The source code from MainActivity.java creating the notification is displayed below:
Button showButton = (Button) findViewById(R.id.show);
showButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
appIntent.putExtra("my_data", 12345);
String question = getString(R.string.the_question);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle(question)
.setContentText(question)
.setTicker(question)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
mManager.notify(NOTIFY_ID, notification);
}
});
My question is: how to modify the notification, so that the user is asked a Yes/No question (in this case: "Do you want to open the car?") and - after she selects Yes or No to launch the same app and run a corresponding method in it (in this case: openCar() or closeCar() method).
I probably should use NotificationCompat.Action.Builder - but how exactly?
Also I am not really sure if this code is the correct code for launching an app from notification and what flags should I use:
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And finally I wonder if hardcodidng some random number in NOTIFY_ID is the correct way when posting notifications?
Here is a source code I used for notification with Login/Register action.
private void sendNotification(String message, String title) {
try {
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);
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent thirdActivityIntent = new Intent(this, ThridActivity.class);
PendingIntent thirdActivityPendingIntent = PendingIntent.getActivity(this, 0 , thirdActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_3d_rotation_white_36dp)
.setContentTitle(title)
.setContentText(message)
.addAction(R.drawable.ic_lock_open_cyan_600_24dp,"Login",secondActivityPendingIntent)
.addAction(R.drawable.ic_lock_pink_700_24dp,"Register",thirdActivityPendingIntent)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
To use it: simply call this method sendNotification(String yourMessage, String yourTitle)
e.g. sendNotification("Hello Message", "Hello Title")
Here is a snapshot of the output
Notify user on pending Intent.. an example is here..
public void notifyUser() {
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher,
"Question.....?????", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "title",
"Explanation of question..", pendingIntent);
// 10 is a random number I chose to act as the id for this notification
notificationManager.notify(10, notification);
}
I'm trying to set an icon to the status bar, I can not view it as soon as you click on the button.
The problem there is the option to delete. I want to set up so you will not be deleted as long as those entering the application and delete
public void setNotificationToStatusBar(){
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
}
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
/** I ADD THIS **/ n.flags=Notification.FLAG_ONGOING_EVENT;
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,0);
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
This is how I create a notification. Using setOngoing(true) I make it persistent
http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)
Intent intent = new Intent("MY_INTENT");
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Click here")
.setSmallIcon(R.drawable.img2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img1))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
If you want to keep your own way of creating the notification and not use the Notification.Builder, you could add this line of code to edit the flag field of the notification
notification.flags |= Notification.FLAG_ONGOING_EVENT;
you can do like this too
int icon = R.drawable.icon_notification;
String msg= "hey";
Intent notificationIntent = new Intent(context,Activity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setPriority(PRIORITY_LOW)
.setContentText(msg)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());