sisko
sisko

Reputation: 9900

GCM notification executes only once

I am successfully receiving notifications from my GCM server to my Android app.

When I select my notification the first time, my notification launches the target activity. However, the following notifications don't launch the activity.

The following is my GCMIntentService class:

public class GCMIntentService extends GCMBaseIntentService{
    private String TAG = "RT-GCM";

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Log.e(TAG, "GCM-SERVICE: error");
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        // TODO Auto-generated method stub
        int nid = Integer.parseInt(intent.getStringExtra("nid"));
        
        try {
//          generateNotification(context, nid, intent.getStringExtra("message"));
            generateNotification(context, intent);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onRegistered(Context context, String arg1) {
        // TODO Auto-generated method stub
        Log.e(TAG, "GCM-SERVICE: Registered");
        
        AccountManager accountManager = AccountManager.get(context);
        accountManager.getAccounts();
        Account account = getAccount(accountManager);
        
        new GCMServerCall().register(arg1, "Tony", account.name);
    }

    @Override
    protected void onUnregistered(Context arg0, String regID) {
        // TODO Auto-generated method stub
        Log.e(TAG, "GCM-SERVICE: UN-Registered");
        new GCMServerCall().unregister(regID);
    }
    
    /**
     * Issues a notification to inform the user that server has sent a message.
     * 
     * I copied this code fragment from a GCM.zip example file
     */
    private static void generateNotification(Context context, Intent myintent) {
        
        Integer nid = Integer.parseInt(myintent.getStringExtra("nid"));
        
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, myintent.getStringExtra("message") + "["+nid+"]", when);
        String title = context.getString(R.string.app_name);
        
        Intent notificationIntent = new Intent(context, ArticleReader.class);
        Log.e("TESTING ...", "Sending NID#" + nid);
        notificationIntent.putExtra("NID", nid);
        notificationIntent.putExtra("nid", nid);
        notificationIntent.putExtra("test", "working");
        
        // 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, myintent.getStringExtra("message") + "["+nid+"]", intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults = Notification.DEFAULT_ALL;
        notificationManager.notify(0, notification);
    }
    
    /****
     * retrieving Google account(s) registered on the target device
     * @param accountManager
     * @return
     */
    private Account getAccount(AccountManager accountManager){
        Account[] accounts = accountManager.getAccountsByType("com.google");
        Log.i(TAG, "We have " + accounts.length + " accounts!");
        return accounts[0];
    }

}

Update

I have discovered something new. After the first and only successful launching of the ArticleReader.class by selecting a notification, selecting following notifications only work if I navigate the app away from ArticleReader. In other words, if an instance of ArticleReader is the active view, any notifications that attempts to launch the same activity won't work.

How can I launch the ActivityReader class repeatedly via notifications even if ActivityReader activity is the current view?

Upvotes: 0

Views: 631

Answers (2)

kumar_android
kumar_android

Reputation: 2263

From docs

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. id - An identifier for this notification unique within your application.

Instead of 0 you should always generate a unique id to get the different notification on your status bar, use time zone for unique identifier

Upvotes: 1

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

I go through your code and it was so perfect for me and I was really wondering what is wrong with it until I find that your IntenetSerivce has no constructor which it should does, and it would be something like the following :

public GCMIntentService(){
        super(PROJECT_ID);
    } 

where the PROJECT_ID is the id of your project that you get from Google API console.

Upvotes: 0

Related Questions