>服务器正在发送数据(推送(通知,作为客户端,我正在使用FirebaseMessagingService和onMessageRecieved方法来制作通知,并且在前台和后台工作良好,但是当我的手机关闭(没有互联网(并建立连接时,最后的通知仅传递到Android手机。
public class MyFirebaseMessagingService extensions MessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String imageUrl = (String) data.get("image");
String action = (String) data.get("action");
Log.i(TAG, "onMessageReceived: title : "+title);
Log.i(TAG, "onMessageReceived: message : "+message);
Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
Log.i(TAG, "onMessageReceived: action : "+action);
if (remoteMessage.getData().isEmpty()){
Log.e(TAG , "Empty");
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();
Intent intent = new Intent(this, UserHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse.test";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("LawyerHome Channel");
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title).setContentText(body).setContentInfo("info")
.setContentIntent(pendingIntent);
notificationManager.notify(new Random().nextInt() , builder.build());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse";
Intent intent = new Intent(this, UserHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("LawyerHome Channel");
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title).setContentText(body).setContentInfo("info").setContentIntent(pendingIntent);
notificationManager.notify(new Random().nextInt() , builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.d("TOKEN" , s);
}
}
这似乎是预期的行为,如果您的应用程序未处于活动状态,则通知将直接发送到系统托盘,并且不会调用您的onMessageReceived()
https://firebase.google.com/docs/cloud-messaging/android/receive