我在firebase上创建推送通知应用程序,一切正常,现在,我想在我的应用程序中添加徽章以增加(图标上的+1),每次我收到新通知...我不知道如何要做到这一点... 这是我的代码
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size() >0){
type="json";
sendNotificatio(remoteMessage.getData().toString());
}
if(remoteMessage.getNotification() !=null) {
type = "message";
sendNotificatio(remoteMessage.getNotification().getBody());
}
}
public void sendNotificatio(String messageBody) {
String id="";
String message="";
String titles="";
if (type.equals("json")){
try {
JSONObject jsonObject = new JSONObject(messageBody);
id=jsonObject.getString("id");
message=jsonObject.getString("message");
titles= jsonObject.getString("title");
} catch (JSONException e) {
e.printStackTrace();
}
} else if (type.equals("message")) {
message= messageBody;
}
Intent i = new Intent(FCM_service.this,Mensage.class);
i.putExtra("id",id);
i.putExtra("message",message);
i.putExtra("titles",titles);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(titles)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
Log.d("LOGTA", "NOTIFICACION RECIBIDA");
Log.d("LOGTAG", "Título:" + titles);
Log.d("LOGTAG", "Texto: " + message);
String dato = message;
i.putExtra("MENSAJE", dato);
Log.e("Mensajito", dato);
}
答案 0 :(得分:0)
您必须优先存储当前计数,并在onMessageReceived
发出通知后,然后必须发送broadcast Receiver
一个优先计数的一个增量值。并接收您想要播放的广播。