[英]Using GCM service in android
What I am doing: 我在做什么:
What is happening: 怎么了:
What I am trying to do: 我正在尝试做的是:
GCMNotificationIntentService.java GCMNotificationIntentService.java
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
//sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
//sendNotification("Deleted messages on server: "+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
//sendNotification("Message Received from Google GCM Server: "+ extras.get(Keys.MSG_KEY));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
GcmBroadcastReceiver.java GcmBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(context, intent);
}
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
/*Toast.makeText(context, "notification_Recieved", Toast.LENGTH_LONG)
.show();*/
}
private void handleMessage(Context context, Intent intent) {
if(intent.getExtras()!=null){
Bundle message = intent.getExtras();
String s= message.getString("the_message");
final Intent emptyIntent = new Intent(context,ActMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1234, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_logo)
.setContentTitle(context.getResources().getString(R.string.screenname_apptitle))
.setContentText(s)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_SOUND | Notification.FLAG_AUTO_CANCEL;
mBuilder.setDefaults(defaults);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1234, mBuilder.build());
/*Toast.makeText(context.getApplicationContext(),
"\n message : " + message, 1).show();*/
NotificationManager objNotfManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
}
}
}
It is replacing the old one because the notification id is same for all notification. 它正在替换旧的通知,因为所有通知的通知ID都相同。 So when you try to add a new notification it replaces the old one instead of adding a new one.
因此,当您尝试添加新的通知时,它将替换旧的通知,而不是添加新的通知。
notificationManager.notify(1234, mBuilder.build());
this line is the problem and you are sending 1234 as notification id for all notifications, replace this line with notificationManager.notify(uniqueId, mBuilder.build());
这行是问题所在,您要发送1234作为所有通知的通知ID,请用
notificationManager.notify(uniqueId, mBuilder.build());
替换此行notificationManager.notify(uniqueId, mBuilder.build());
If you want diferent notification for different push add different notification id. 如果您希望不同的推送通知不同,请添加不同的通知ID。 You can set a inbox mode to the notifications as well so that a group of notifications are visible under one just like gmail see this link
您还可以将收件箱模式设置为通知,以便在一组通知下可以看到一组通知,就像gmail看到此链接一样
For better understanding of notification see this 为了更好地了解通知,请参阅此
try changing this 尝试改变这个
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1234, mBuilder.build());
to 至
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(System.currentTimeMillis(), mBuilder.build());
It should have a unique number. 它应该有一个唯一的数字。 Hope this will help
希望这会有所帮助
Date date = new Date();
int notification_id = (int) date.getTime();
notificationManager.notify(notification_id, notification);
use timestamp id instead of final value for notification id 使用时间戳ID代替通知ID的最终值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.