Android 应用程序被终止时未接收firebase通知
经过大量研究,我已经正确地实现了FCM,即我使用了数据负载而不是通知。我已经添加了完整的代码,请告诉我哪里出错了 MyFirebaseInstanceService:Android 应用程序被终止时未接收firebase通知,android,firebase-cloud-messaging,firebase-notifications,Android,Firebase Cloud Messaging,Firebase Notifications,经过大量研究,我已经正确地实现了FCM,即我使用了数据负载而不是通知。我已经添加了完整的代码,请告诉我哪里出错了 MyFirebaseInstanceService: package com.example.student.notitest.Service; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import
package com.example.student.notitest.Service;
import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.util.Log;
import com.example.student.notitest.R; import com.google.firebase.iid.FirebaseInstanceIdService; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage;
import java.util.Map; import java.util.Random;
public class MyFirebaseInstanceService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
else
showNotification(remoteMessage.getData());
}
private void showNotification(Map<String, String> data) {
String title = data.get("title").toString();
String body = data.get("body").toString();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.student.notitest";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Quiz App");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_notification)
.setTicker("India Quiz")
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.student.notitest";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("India Quiz App Notifications Here");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_notification)
.setTicker("India Quiz")
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("TOKENFIREBASE",s);
} }
package com.example.student.notitest.Service;
导入android.app.Notification;导入android.app.NotificationChannel;导入android.app.NotificationManager;导入android.content.Context;导入android.graphics.Color;导入android.os.Build;导入android.support.v4.app.NotificationCompat;导入android.util.Log;
导入com.example.student.notitest.R;导入com.google.firebase.iid.firebaseInstancedService;导入com.google.firebase.messaging.FirebaseMessagingService;导入com.google.firebase.messaging.RemoteMessage;
导入java.util.Map;导入java.util.Random;
公共类MyFirebaseInstanceService扩展了FirebaseMessagingService{
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
其他的
showNotification(remoteMessage.getData());
}
私有无效显示通知(地图数据){
字符串title=data.get(“title”).toString();
String body=data.get(“body”).toString();
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
字符串通知\u CHANNEL\u ID=“com.example.student.notitest”;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel NotificationChannel=新的NotificationChannel(通知通道ID,“通知”,
通知经理(重要性高);
notificationChannel.setDescription(“测验应用程序”);
notificationChannel.enableLights(真);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此为通知通道ID);
notificationBuilder.setAutoCancel(真)
.setDefaults(通知.DEFAULT\u全部)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_通知)
.setTicker(“印度测验”)
.setContentTitle(标题)
.setContentText(正文)
.setContentInfo(“信息”);
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
私有void showNotification(字符串标题、字符串正文){
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
字符串通知\u CHANNEL\u ID=“com.example.student.notitest”;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel NotificationChannel=新的NotificationChannel(通知通道ID,“通知”,
NotificationManager.重要性(默认值);
notificationChannel.setDescription(“此处为印度测验应用程序通知”);
notificationChannel.enableLights(真);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此为通知通道ID);
notificationBuilder.setAutoCancel(真)
.setDefaults(通知.DEFAULT\u全部)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_通知)
.setTicker(“印度测验”)
.setContentTitle(标题)
.setContentText(正文)
.setContentInfo(“信息”);
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
@凌驾
公共void onNewToken(字符串s){
super.onNewToken(s);
Log.d(“火基令牌”,s);
} }
我也尝试过通过REST客户端发送通知,例如ARC(google exension),但没有成功。请提供帮助,因为就同一主题编写代码已超过3天,但尚未找到任何解决方案。
请帮忙 你是如何杀死你的应用程序的?你是否在清单文件中注册了
FirebaseMessagingService
?是的,我添加了@Jaymintry以便在onMessageReceived
中放置一些日志程序?你是否将google services.json
添加到你的应用程序
模块目录中?