gpt4 book ai didi

android - 当我从 GCM 服务器收到通知时如何更新操作栏上的通知图标

转载 作者:行者123 更新时间:2023-11-29 20:55:02 25 4
gpt4 key购买 nike

当我从服务器收到通知时如何更新通知图标?这是我从服务器获取通知的代码。

 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);
}
}

最佳答案

将此值更改为所需的可绘制图像

.setSmallIcon(R.drawable.assignedtickets)

要更新操作栏图标,您需要获取 Activity 实例

如果你在 Activity 中,那么

this.getActionBar.setIcon()

如果在 fragment 中则

getActivity.getActionBar.setIcon()

关于android - 当我从 GCM 服务器收到通知时如何更新操作栏上的通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com