In Samsung S4, App is not opening when we click on notification - android

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.

Related

How to reload phonegap index.html file on notification click

I done an android application using Phonegap and Java. I got notification successfully in application , after clicking notification app is start from background.I want to reload my Index.html file after notification click. How can i handle this issue ?
Please help
thanks in advance.
thanks mit, here is my generate notification code
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, MainActivity.class);
notificationIntent.putExtra("data1", "data1");
MainActivity ObjMain=new MainActivity();
ObjMain.sendJavascript("TestFunc");
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent contentIntent =PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
if (message!="")
{
notificationManager.notify(0, notification);
}
}

update an activity when click on notification

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

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

Android Application crashes on some devices on notification receives

I am using the following code for creating a notification in Android. But its working fine in some devices. But when I checked in Samsung Grand, it crashes while receiving the notification. Android version 4.1.2 ( both devices )
private static void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = "From Application";
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("message", message);
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;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, notification);
}
Any idea ? I am new to Android..
Thanks in advance

How to resolve notification issue in android?

I am developing notification related things.Problem is click on button. I wrote the following notification related code :
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"New update received", when);
Intent notificationIntent = new Intent(context,
MessageReceivedActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle=new Bundle();
bundle.putString("post_title", message);
notificationIntent.putExtras(bundle);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "Agility Logistics", "New update received", intent);
// notification.number +=1;
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
If button is clicked, the button onclick notification is overriding all the other notifications.This is undesirable. So how can I show all notifications received without having them overwritten by button click notification ?

Categories

Resources