作者:人一旦_488 | 来源:互联网 | 2023-07-03 09:03
我的应用程序被杀死但我没有找到任何能实现此功能的代码时,我不需要收到通知。
我可以在前景和背景中接收通知,但是当我终止该应用程序时,它们仍然会到达。
public void onmessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG,"From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG,"Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
try {
Log.d(TAG,"Message Notification Body: " + remoteMessage.getData().get("body"));
}
catch (Exception e){
}
}
//The message which i send will have keys named [message,image,Anotheractivity] and corresponding values.
//You can change as per the requirement.
try {
//message will contain the Push Message
String message = remoteMessage.getData().get("body");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
String titulo = remoteMessage.getData().get("title");
String sidusuario = remoteMessage.getData().get("sidusuario");
String idnotificaciOnusuario= remoteMessage.getData().get("idnotificacionusuario");
sendNotification(message,imageUri,titulo);
}
catch (Exception e){
e.printStackTrace();
}
}
这是我的 sendNotification方法
void sendNotification(String messageBody,String icon,String titulo)
{
String channelId = "General";
Uri defaultsoundUri = RingtoneManager.getDefaulturi(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificatiOnBuilder=
new NotificationCompat.Builder(this,channelId)
.setLargeIcon(getBitmapFromURL(icon))
.setContentTitle(titulo)
.setSmallIcon(R.drawable.logo_blanco_chapur)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigTextStyle())
.setautoCancel(true)
.setsound(defaultsoundUri);
Notificationmanager notificatiOnmanager=
(Notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,"Channel human readable title",Notificationmanager.IMPORTANCE_DEFAULT);
notificationmanager.createNotificationChannel(channel);
}
notificationmanager.notify(getID(),notificationBuilder.build());
}
{
“ to”:“令牌”,
“通知”:{
“ title”:“ title”,
“ body”:“ **** body ****。”,
“ mutable_content”:是
}
"data": {
"url": "https://upload.wikimedia.org/wikipedia/commons/8/80/Coiled_Galaxy.jpg","title": "title","body": "****body****."
}
}
它可能重复here。 重要提示:如果应用在后台运行或被杀死,则要收到通知,有效负载必须删除notification
属性