如果用户没有在前台单击通知(通常应该发生),我想打开我的应用程序。当我发送FCM display-message
时,该应用程序在单击通知后打开,但是当我发送data-message
时,该应用程序在单击通知时未打开。
这是 MyAndroidFirebaseMsgService 类:
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
Bitmap bitmap;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().get("imgUrl") == null && remoteMessage.getData().get("webUrl") == null) {
createNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
} else if (remoteMessage.getData().get("imgUrl") != null && remoteMessage.getData().get("webUrl") != null) {
String imageUrl = remoteMessage.getData().get("imgUrl");
String websiteUrl = remoteMessage.getData().get("webUrl");
createNotificationBoth(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), imageUrl, websiteUrl);
} else if (remoteMessage.getData().get("imgUrl") != null) {
String imageUrl = remoteMessage.getData().get("imgUrl");
//createNotificationWithImage(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), imageUrl);
createNotificationWithImage(remoteMessage.getData().get("body"), remoteMessage.getData().get("title"), imageUrl);
} else if (remoteMessage.getData().get("webUrl") != null) {
String websiteUrl = remoteMessage.getData().get("webUrl");
createNotificationWithUrl(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), websiteUrl);
} else {
String body = "Nothing Received";
String title = "No Title";
createNotification(body, title);
}
}
private void createNotificationWithUrl(String messageBody, String messageTitle, String websiteUrl) {
Intent intent = new Intent( this , MyAndroidFirebaseMsgService.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(getApplicationInfo().icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
private void createNotificationWithImage(String messageBody, String messageTitle, String imageUrl) {
bitmap = getBitmapFromUrl(imageUrl);
Intent intent = new Intent( this , MyAndroidFirebaseMsgService.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(getApplicationInfo().icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
private void createNotificationBoth(String messageBody, String messageTitle, String imageUrl, String websiteUrl) {
bitmap = getBitmapFromUrl(imageUrl);
Intent intent = new Intent( this , MyAndroidFirebaseMsgService.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(getApplicationInfo().icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
private void createNotification( String messageBody, String messageTitle) {
Intent intent = new Intent( this , MyAndroidFirebaseMsgService.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(getApplicationInfo().icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
发送到应用程序的数据有效载荷:
$fields = array(
'registration_ids' => $registration_ids,
'data' => array(
"title" => $message['title'],
"body" => $message['message'],
"imgUrl" => $message['imgUrl'],
)
);
顺便说一句,我只想发送data-message
,因为我希望它在应用程序处于后台时显示BigImage
。