public class AppFireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
else if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
PreferenceHelper preferenceHelper = new PreferenceHelper(this);
preferenceHelper.putString(Constants.FCM_TOKEN, token);
}
public void sendNotification(String title, String messageBody) {
try {
ObjectMapper mapper = new ObjectMapper();
Notification notification = mapper.readValue(messageBody, Notification.class);
Intent intent = null;
if (notification.getPostType().equalsIgnoreCase("message")) {
intent = new Intent(this, MessageActivity.class);
intent.putExtra(Constants.LOGIN_ID, notification.getLoginIdEnc());
} else {
intent = new Intent(this, HomeActivity.class);
}
Toast.makeText(this, notification.toString(), Toast.LENGTH_SHORT).show();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(notification.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
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(0 /* ID of notification */, notificationBuilder.build());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
}
如果推送通知在有效载荷中有通知部分,而您的app在后台,则永远不会调用onMessageReceived。通知由Firebase自动创建和显示。没有办法截取此信息并修改通知或为此进行任何其他操作。只有点击通知后,您的应用程序才会获得控制权。
确保在所有情况下(前台、后台和应用被杀死)都调用onMessageReceived的唯一方法是从FireBase发送一个仅数据的有效负载。
请查看我对一个类似问题的回答以了解更多细节。
onMessageReceived(RemoteMessage RemoteMessage)在应用程序处于后台时未调用或仅显示{“body”:“test”,“title”:“test notification}之类的json字符串
我创建了一个自定义后台服务,它正在扩展IntentService。当我的应用程序进入后台时,我会调用这个类。startService()。Firsly onStartCommand在我返回START_STICKY的地方被调用(就像我读过一些其他帖子,以保持我的服务处于活动状态,并在由于内存不足而被终止时根据需要重新创建)。在那之后,我将实现onHandleIntent(intent)方法,从调用者
我正在构建一个应用程序,即使应用程序在后台,也需要保持位置跟踪。基本上,我正在使用播放服务的GoogleApiClient和LocationServices。FusedLocationApi.request位置更新方法来调用IntentService。 下面的代码是我如何在活动的后台触发位置更新: 效果很好 但是,当用户(通过任务管理器)终止应用程序时,我想停止此IntentService(称为L
在应用程序处于活动状态时,调用DidReceiveEmotentification方法,当应用程序处于后台和非活动状态时,不调用DidReceiveEmotentification方法。 代码:- Func应用程序(_应用程序:UIApplication,didReceiveRemote teNotify userInfo:[AnyHasable:任何],fetchCompletionHandle