0

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
AndroidFan
  • 41
  • 7
  • *it does not redirect to desired activity* ... could you be more precisely ? also `WebActivity.isActivtyRunning` ??? why don't you just forget about this and use activity flags + onNewIntent? – Selvin Jun 24 '15 at 11:15
  • @Selvin i am redirecting it to NoticeActivity, as you can see generateNotificationNotice() method in my code. But when i taps on notification , notification get hidden from status bar and NoticeActivity is not started. – AndroidFan Jun 24 '15 at 11:18
  • Selvin i have updated my code please check that – AndroidFan Jun 24 '15 at 11:21
  • did you tried [this](http://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity)? – Selvin Jun 24 '15 at 11:25
  • @Selvin But in my case it is not even opening that NoticeActivity – AndroidFan Jun 24 '15 at 11:28
  • try to add `Intent.FLAG_ACTIVITY_NEW_TASK` to the `notificationIntent` ... – Selvin Jun 24 '15 at 11:30
  • its not working , not opening NoticeActivity – AndroidFan Jun 24 '15 at 12:09

0 Answers0