如何从服务器获取通知时更新通知图标?这是我从服务器获取通知的代码。
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;
}
}
下面是GCMintentService的代码,我收到消息并显示通知。
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);
}
}
答案 0 :(得分:1)
将此值更改为所需的可绘制图像
.setSmallIcon(R.drawable.assignedtickets)
要更新操作栏图标,您需要获取活动实例
如果你参加活动,那么
this.getActionBar.setIcon()
如果是片段那么
getActivity.getActionBar.setIcon()
答案 1 :(得分:1)
一旦您从Android客户端设备上的GCM服务器收到通知,您就可以更新通知栏上的通知,该通知与通过互联网接收的任何其他通知的通知相同。一旦您的WakefulBroadcastListener收到通知,应用程序代码将如何处理。
有关代码实现,请参阅以下Stack Overflow issue。
如果您获得invalidateoptionsmenu返回null,请创建一个新的帮助程序类
class VersionHelper
{
static void refreshActionBarMenu(Activity activity)
{
activity.invalidateOptionsMenu();
}
}
现在在上面的代码中,替换invalidateOptionsMenu();用:
if (Build.VERSION.SDK_INT >= 11)
{
VersionHelper.refreshActionBarMenu(this);
}
希望这会有所帮助!!