推送通知不与FCM一起使用

编程入门 行业动态 更新时间:2024-10-27 09:47:20
本文介绍了推送通知不与FCM一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在我的Android应用中使用Firebase获取推送通知。问题是当应用程序在前台,我收到通知,并调用 onMessageReceived(),但是当我在后台时,我没有收到任何通知

我做错了什么?

Manifest.xml $ b

< service android:name =。 notifications.MyFirebaseMessagingService> < intent-filter android:priority =2147483647> < action android:name =com.google.firebase.MESSAGING_EVENT/> < / intent-filter> < / service>

MyFirebaseMessagingService.class

public class MyFirebaseMessagingService extends FirebaseMessagingService { $ b @Override public void onCreate(){ super.onCreate(); Log.e(MessagingService,onCreate Firebase服务); $ b @Override public void onMessageReceived(RemoteMessage remoteMessage){ Log.e(MessagingService,onMessageRecieved); String body = remoteMessage.getData()。get(body); ooVooSdkSampleShowApp application =(ooVooSdkSampleShowApp)getApplication(); Intent intent = new Intent(application.getContext(),MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(application.getContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder(application.getContext()); b.setAutoCancel(false) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.image_calendar_red ) .setContentText(body) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) .setContentIntent(contentIntent) .setContentInfo(Info); NotificationManager notificationManager =(NotificationManager)application.getContext()。getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1,b.build()); $ b $ / code $ / $ p

解决方案

算出来了。这是由Firebase SDK处理服务器响应的问题。我通过服务器端的通知字段发送通知。

通知 =只在应用程序处于前台时显示通知。这是Firebase SDK / GCM SDK的规则

$ b 数据 =当应用程序处于后台时显示通知。这是Firebase SDK / GCM SDK的规则。

在服务器端,我这样做了:

body:JSON.stringify({ notification:{ data:{ title:message }, to :'/ topics / user _'+ username

更多信息

I am trying to get push notifications using Firebase in my Android app. The problem is when the app is in the foreground, I receive the notification and onMessageReceived() is called, however when I am on the background, I don't receive any notification and the onMessageRecieved isn't called.

What am I doing wrong?

Manifest.xml

<service android:name=".notifications.MyFirebaseMessagingService"> <intent-filter android:priority="2147483647"> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>

MyFirebaseMessagingService.class

public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onCreate() { super.onCreate(); Log.e("MessagingService", "onCreate Firebase Service"); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.e("MessagingService", "onMessageRecieved"); String body = remoteMessage.getData().get("body"); ooVooSdkSampleShowApp application = (ooVooSdkSampleShowApp) getApplication(); Intent intent = new Intent(application.getContext(), MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(application.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder(application.getContext()); b.setAutoCancel(false) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.image_calendar_red) .setContentText(body) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) .setContentIntent(contentIntent) .setContentInfo("Info"); NotificationManager notificationManager = (NotificationManager) application.getContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, b.build()); } }

解决方案

Figured it out. It was the problem with the way server response being handled by Firebase SDK. I was sending notification using "notification" field from the server side.

Notification = Just show Notification when the app is in foreground. This is the rule for Firebase SDK/ GCM SDK

Data = Show Notification when the app is in background. This is the rule for Firebase SDK/GCM SDK.

On server side, I have done it like this:

body: JSON.stringify({ notification: { data: { title: message }, to : '/topics/user_'+username

Further Info

更多推荐

推送通知不与FCM一起使用

本文发布于:2023-11-17 03:46:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不与   通知   FCM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!