I am using push notifications in Android. When I receive a push notification and click on it it launchs an activity as i want , but if this activity it's already opened nothing happened , the activity keeps the old data.
AndroidManifest.xml
<activity
android:name=".Detail"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_detail"
android:launchMode="singleTop"
android:parentActivityName=".Home"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ma.via.marocagenda.Home" />
</activity>
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
...
private static void generateNotification(Context context, String message) {
//int icon = R.drawable.abc_ic_go;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Detail.class);
// put extra for details
notificationIntent.putExtra("id", VAL_TAG_ID);
notificationIntent.putExtra("titre", VAL_TAG_TITRE);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
//PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
what can be wrong ?
You Can override onNewIntent() in your Activity to get the Latest Intent From the Notification.
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("key")){
//do your Stuff
}
}
You may use Local broadcasts from your service whenever a push notification arrives.
For how to use Local broadcasts read here https://stackoverflow.com/a/8875292/826657
Let your activity register for this broadcast action, and when a push notification is received, update the activity from onReceive().
Also, being more specific using ActivityManager you can check that if you activity is on top, if true send the broadcast also, else just show the notification.
Read here for how to find the current foreground activity.
https://stackoverflow.com/a/4753333/826657
Related
I have a task in which I have to show Notification for an incoming message inside an activity using BroadcastReceiver with the address and content on the SMS. Now I have to click in order to insert the values in sms in database. When the task is finished i want it to clear from the activity. Is it possible to create notification for incoming sms inside an Activity?
You can Try This no generate your own Notification.
#SuppressWarnings("deprecation")
private static void generateNotification(Context context) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "New Contact Added",
when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent intent = PendingIntent.getActivity(context, iUniqueId,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, "New", 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(2, notification);
}
In my notification, I want to start current activity when click in pending intent. In google, many ways found and I tried many times, it didn't work. here is my code,
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String startTime = intent.getStringExtra("Strar_time");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
notification.vibrate = new long[]{100, 200, 100, 500};
notification.defaults = Notification.DEFAULT_ALL;
Intent notificationIntent = new Intent(this, Goo.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
notification.contentIntent=contentIntent;
nm.notify(NOTIF_ID, notification);
NOTIF_ID++;
Log.i("NotiID",NOTIF_ID+"");
}
Try this function given below,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.launch_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, Main.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, 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(0, notification);
}
Only one working way I found so far is to make a dummy activity, start it and finish it in onRusume(). However this technique only bring application to front and do not start application when it is closed.
Then I used following workaround to bring application on front if it is alive in background otherwise start it.
Manifest:
<application>
.
.
.
<activity
android:name=".DummyActivity"
/>
.
.
</application>
In code:
public class DummyActivity{
public static boolean isAppAlive;
#Override
protected void onResume() {
// start application if it is not alive
if(!isAppAlive){
PackageManager pmi = getPackageManager();
Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);
if (intent != null){
startActivity(intent);
}
}
finish();
}
In your main activity's onCreateMethod.
#Override
public void onCreate(Bundle savedInstanceState) {
DummyActivity.isAppAlive = true;
}
It is obvious that when your application is fully closed, isAppAlive will restore to its default value which is false. Thus application will be start.
In my app I am sending GCM notification to device, when we click on notification it is not opening app in Samsung S4. Please any one help me.
Code:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, Main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
String title = context.getString(R.string.app_name);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0111, notification);
Add android:exported="true" in the AndroidManifest for the Activity specified by the intent.
This problem is dependent of Android Version not Samsung.......
Add android:exported = "true" in the manifest
<activity
android:name=".G_chat"
android:label="G_chat"
android:exported="true">
</activity>
G_chat is the activity which will be opened on the notification click.
I'm creating an application that use Google GCM Service.
So I defined this function:
#Override
protected void onMessage(Context context, Intent intent)
onMessage calls a function, this is part of code:
Intent notificationIntent = new Intent(context,ShowMess.class);
notificationIntent.putExtra("id",current_id);
notificationIntent.putExtra("cat", cat);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(id, notification);
If I send a message to myself (with AsyncTask) but close fastly the applicaiton (Back Button) next messages will not launch activity (ShowMEss.class) Why in your opinion?
Bye
Thanks
Your above code has not registered the notification. Are you missing following part in your code?
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
I've an app that i can push notifications on to via GCM. I receive the notification in the notification drawer but when i click on it, an activity from the app is started(EntryActivity). What i'd like to happen is if the message in the notification is a url to the .apk file eg 'http://path_to_file.apk', then i'd like the file to start downloading.
Below is the code that generates the notification, is there something i need to add or change to start the download?
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, EntryActivity.class);
// 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, 0);
notification.setLatestEventInfo(context, title, message, 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(0, notification);
}
Try this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://path_to_file.apk"));
notificationIntent.setClassName("com.android.browser",
"com.android.browser.BrowserActivity");