gpt4 book ai didi

java - 在 android 中使用 GCM 服务

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

我在做什么:

  • 我正在使用推送通知来接收通知。
  • 我已经使用了下面的代码
  • 我也在 list 中声明了必要的权限

发生了什么:

  • 我能够收到通知
  • 但是当新的通知到来时,旧的通知被替换

我正在尝试做的事情:

  • 说收到第一个通知
  • 当第二个通知出现时旧的不应该被替换相反,两者都应该显示

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

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

最佳答案

它正在替换旧的,因为所有通知的通知 ID 都相同。因此,当您尝试添加新通知时,它会替换旧通知而不是添加新通知。

notificationManager.notify(1234, mBuilder.build());

这一行是问题所在,您要发送 1234 作为所有通知的通知 ID,将这一行替换为notificationManager.notify(uniqueId, mBuilder.build());

如果你想为不同的推送添加不同的通知id。您也可以为通知设置收件箱模式,以便在一个通知下显示一组通知,就像 gmail 一样,请参阅此 link

为了更好地理解通知,请参阅 this

关于java - 在 android 中使用 GCM 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555771/

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