I am developing an android application that uses GCM Push notification to alert user about various events. I have successfully done that part. Now I have some strange issue that basically has 2 cases. First one is that , when i receive a message through GCM , i am generating a status bar notification , when user taps on it, it successfully redirects to desired target Activity that i had used in my intent object. This happens when Application is started from phone menu.
Now Second case, that have actual problem is like this : When i am out of my application or my application is not visible or running , when i receive a message through GCM , i am generating a status bar notification , when user taps on it, it redirects to target activity as usual. After that if any further messages comes and user taps on status bar notification , it does not redirect to desired activity. I am using same code for generating notification but it is not working in second case.
Here is My Code:
public class GCMIntentService extends GCMBaseIntentService {
public static String SENDER_ID = "842798625522";
static final String DISPLAY_MESSAGE_ACTION = "com.test.test.DISPLAY_MESSAGE";
static final String DISPLAY_NOTICE_ACTION = "com.test.test.DISPLAY_NOTICE";
public static boolean isNoticeActive = false;
static final String EXTRA_MESSAGE = "message";
static int noticeCount = 0;
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("//////// error " + arg1);
}
@Override
protected void onMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
String message = intent.getExtras().getString("message");
if (message != null && message.trim().length() > 0) {
try {
JSONObject json = new JSONObject(message);
if (json != null) {
String type = json.getString("type");
if (type.equalsIgnoreCase("chat")) {
// for chatting
} else {
noticeCount = SimpleUtility.getPreferenceCount(context,
"key");
noticeCount = noticeCount + 1;
SimpleUtility.setPreferenceNoticeCount(context, "key",
GCMIntentService.noticeCount);
displayNotice(context, message);
if (!(NoticeActivity.isNoticeActivityRunning)) {
generateNotificationNotice(context, message);
}
} // else notice
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// if ends
} // method ends
@Override
protected void onRegistered(Context context, String gcmId) {
// TODO Auto-generated method stub
SimpleUtility.setGCMKey(context, "key", gcmId);
System.out.println("reg");
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Toast.makeText(context, "Gcm is unregistered", Toast.LENGTH_LONG)
.show();
System.out.println("gcm id is " + registrationId);
}
@SuppressWarnings({ "deprecation" })
private static void generateNotificationNotice(Context context,
String message) {
String notificationMessage = "";
if (message != null && message.trim().length() > 0) {
try {
JSONObject json = new JSONObject(message);
if (json != null) {
notificationMessage = json.getString("mgs");
// json.getString("title");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int icon = R.drawable.app_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, notificationMessage,
when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, NoticeActivity.class);
notificationIntent.putExtra("type", "notification");
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, notificationMessage,
intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
}
static void displayNotice(Context context, String message) {
Intent intent = new Intent(DISPLAY_NOTICE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
// context.sendBroadcast(intent);
context.sendBroadcast(intent);
}
} // class ends