ankur arora
ankur arora

Reputation: 161

How to update a notification icon on Action bar when I get Notification from GCM server

How to update the notification icon as i get Notification from server? Here is my code to getting notification from server.

 public class GCMMessageHandler extends GCMBaseIntentService {
    static int notificationcount;
    TAB_Activity tab_Activity;
    int count= 0;
    public GCMMessageHandler() {
        super("GCMMessageHandler");
        // TODO Auto-generated constructor stub
    }
    String message,ticketno,name,status,problem;
    private Handler handler;
    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }



    @Override
    protected void onMessage(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle extras = intent.getExtras();

        GoogleCloudMessaging gcm = new GoogleCloudMessaging().getInstance(this);
        String messagetype= gcm.getMessageType(intent);
        count++;
        System.out.println(extras);
        message = extras.getString("message");
        ticketno= extras.getString("ticket_no");
        problem=extras.getString("problem_desc");
        name= extras.getString("name");
        status= extras.getString("t_status");
        Log.i("GCM   Messge Recieved"," (" +messagetype+")  "+extras.getString("message"));
        GCMintentService.completeWakefulIntent(intent);
        Intent in = new Intent(this, Chat_activity.class);
        // Pass data to the new activity
        Bundle bd = new Bundle();
        bd.putString("TicketID", ticketno);
        bd.putString("problem", problem);
        in.putExtras(bd);

        in.setData(Uri.parse("tel:/"+(int)System.currentTimeMillis()));

        // Starts the activity on notification click
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, in,
        Intent.FLAG_ACTIVITY_NEW_TASK);
        // Create the notification with a notification builder
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
        .setWhen(System.currentTimeMillis())
        .setContentText(message+" :-"+name)
        .setContentTitle(ticketno)
        .setSmallIcon(R.drawable.assignedtickets)
        .setTicker("Smart CRM")
        .setContentIntent(pIntent)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));


Notification notification = notificationBuilder.build();

    manager.notify((int)System.currentTimeMillis(), notification);

    ActivityManager  activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> procinfo = activityManager.getRunningAppProcesses();
        for (int i = 0; i < procinfo.size(); i++) {

            if(procinfo.get(i).processName.equals("com.xsinfosol.helpdesk_customer"))
            {
                notification.number=++notificationcount;
                notificationcount =notificationcount++;
                setBadgeNotify(getApplicationContext(),notificationcount);
                break;

            }

        }




        {
        // Wake Android Device when notification received
        PowerManager pm = (PowerManager) context
        .getSystemService(Context.POWER_SERVICE);
        final PowerManager.WakeLock mWakelock = pm.newWakeLock(
        PowerManager.FULL_WAKE_LOCK
        | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
        mWakelock.acquire();

        // Timer before putting Android Device to sleep mode.
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
        public void run() {
        mWakelock.release();
        }
        };
        timer.schedule(task, 5000);
        }


    }
    @Override
    protected void onRegistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }





     private static void setBadgeNotify(Context context, int count) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return; 
            }

            Intent intent = new Intent();
            intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",launcherClassName );//"com.xsinfosol.helpdesk_customer.MainActivity"
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",context.getPackageName() );//"com.xsinfosol.helpdesk_customer"

            context.sendBroadcast(intent);
        }   


     private static String getLauncherClassName(Context context) {

            PackageManager pm = context.getPackageManager();

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
            for (ResolveInfo resolveInfo : resolveInfos) {
                String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
                if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                    String className = resolveInfo.activityInfo.name;
                    return className;
                }
            }
            return null;
        }



}

Below is the code for GCMintentService where I am getting message and showing notification.

public class GCMintentService extends WakefulBroadcastReceiver {

    private static final String TAG= "GCM Tutorial::Service";

    public static final String senderId="************";

    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comName = new ComponentName(context.getPackageName(), GCMMessageHandler.class.getName()); 
        startWakefulService(context, (intent.setComponent(comName)));
        setResultCode(Activity.RESULT_OK);
}
}

Upvotes: 1

Views: 1163

Answers (2)

AniV
AniV

Reputation: 4037

Once you are getting the notification from your GCM server on your android client device you can update the notification on the notification bar same as that of any other notification received over the internet. Once your WakefulBroadcastListener receives the notification, it is upto the app code how that can be handled.

Please refer to the following Stack Overflow issue for code implementation.

If you are getting invalidateoptionsmenu returning null, Create a new helper class

class VersionHelper
{
    static void refreshActionBarMenu(Activity activity)
    {
        activity.invalidateOptionsMenu();
    }
}

Now in your code above, replace invalidateOptionsMenu(); with:

if (Build.VERSION.SDK_INT >= 11)
{
    VersionHelper.refreshActionBarMenu(this);
}

Hope this would Help!!

Upvotes: 1

Jiju Induchoodan
Jiju Induchoodan

Reputation: 4266

Change this value to the desired drawable image

.setSmallIcon(R.drawable.assignedtickets)

For updating the action bar icon you need to get the activity instance

if you are in activity, then

this.getActionBar.setIcon()

if in fragment then

getActivity.getActionBar.setIcon()

Upvotes: 1

Related Questions