Android Firebase后台通知未触发

问题描述 投票:2回答:3

我正在开发一个从Firebase接收推送通知的Android应用程序。

我可以获得令牌并从Postman发送推送通知而没有任何问题。

如果App位于前台,一切都按预期工作,我在onMessageReceived(我测试了各种有效负载)中收到了有效负载。

但如果我关闭应用程序它不会收到任何东西。我尝试了很多有效载荷,并且我阅读了所有文档(数据和有效载荷中的通知之间的差异)。

这是我的项目使用的我的类:

1 - 扩展FirebaseMessagingService的类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "Android Push App";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification("Received notification");
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.push_icon)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

2 - 负责获取令牌的类

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static String TAG = "Android Push App";

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
    Log.d(TAG, "Did obtained token");
    Log.d(TAG, token);
}

3 - 我的清单:

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="FIREBASE_ACTIVITY" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <service android:name=".push.core.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <service
        android:name=".push.core.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/push_icon" />

    <meta-data android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

</application>

4 - MainActivity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Log.d("MainActivity", "Key: " + key + " Value: " + value);
        }
    }
}

5 - 最后我在邮差中尝试了这个有效载荷

POST https://fcm.googleapis.com/fcm/send Content-Type application / json授权密钥= AIzaSy(...)kY

JSON Body(我试过的例子):

{
"to": "dxe0RDKbP...m9Uc","notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "push_icon",
  "sound" : "default"
}}

和:

{
"to": "dxe0RDKbP...m9Uc","notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "push_icon",
  "sound" : "default"
}}

和:

{
"to": "d3j-9OJ6R...C6w",
"notification" : {
    "title": "title",
    "body": "body"
},
"data": {
    "tipo": "normal"
}}

还添加了“优先级”键,但它不起作用。

我做错了什么?

感谢您给我的所有帮助:)

UPDATE

现在它正在运作。

FireBaseMessagingService与正在运行的Geofence Push(由App触发)之间存在冲突。

删除此Geofence服务后,一切都按预期工作。

还可以在推送的有效负载中使用通知和数据键。

谢谢

android json firebase firebase-cloud-messaging
3个回答
1
投票

试试这个

你不能将JSON密钥'notification'放入你的firebase API请求中,而是使用'data'

用这个

{
  "to": "dxe0RDKbP...m9Uc",
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "push_icon",
    "sound" : "default"
  }
} 

编辑你可以尝试只有身体和标题

{
  "to": "dxe0RDKbP...m9Uc",
   "data": {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark"
   }
} 

编辑新

在清单文件android:stopWithTask="false"服务属性中添加此项。

 <service
        android:name="com.yourapp.YourPushService"
        android:stopWithTask="false"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
 </service>

0
投票

您需要从json中删除通知负载并向其添加数据负载。这是因为Android具有内置功能,可以在看到通知有效负载时处理通知,即,无论何时发送通知有效负载,android直接将其发送到系统dray和onMessageReceived函数,都不会被调用。


0
投票

根据firebase文档,Firebase通知的行为会有所不同,具体取决于接收应用的前景/背景状态。

为大多数消息类型提供onMessageReceived,但以下情况除外:

当您的应用在后台时发送通知消息。在这种情况下,通知将传递到设备的系统托盘。用户点按通知会默认打开应用启动器。

具有通知和数据有效负载的消息,包括后台和前台。在这种情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递。

因此,您需要从json中删除通知负载,并且只需要在应用程序处于后台时保持数据有效负载触发onMessageReceived。

链接:https://firebase.google.com/docs/cloud-messaging/android/receive

© www.soinside.com 2019 - 2024. All rights reserved.