应用程序关闭时 Firebase 推送通知不起作用
Posted
技术标签:
【中文标题】应用程序关闭时 Firebase 推送通知不起作用【英文标题】:Firebase Push Notification not working when app is closed 【发布时间】:2022-01-13 02:10:32 【问题描述】:我是 android 新手,我遇到了这个问题
如果应用程序在前台,则通知正常显示
如果我将应用程序放在后台,通知将显示并且它也可以正常工作
BUT
问题是-> when I close the application(the app is killed) the notification does not show
我尝试过的解决方案
只发送通知 只发送数据 没有为通知放置自定义视图我的理解是,如果我发送only
通知 without data
它将显示应用程序何时处于前台AKA when it's open and you can see it
和
如果我只在没有通知的情况下发送数据,它将在后台显示,当应用程序被终止时,数据将在主要活动意图中找到(但显然这是NOT
我的情况)
我正在使用 android 10 Infinix Note 5 手写笔,以防万一这个型号有问题
但我仍然可以正常看到来自任何其他应用的通知
edit
所以我尝试在其他手机上运行该应用程序,它按预期工作,但在我的手机上,当应用程序关闭时发送的所有通知只会在我打开应用程序时显示
I close the app -> send some notifications -> no thing on the phone ->
open the app again -> all the notifications I sent are are now there
当我在应用程序处于前台时发送通知以及将其置于后台时,它仍然可以正常工作
代码:
public class FirebasePushNotificationConfig extends FirebaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage);
private void sendNotification(RemoteMessage remoteMessage)
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel channel = new NotificationChannel(AppStrings.MSG.name(), "Testing", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(false);
channel.enableLights(true);
manager.createNotificationChannel(channel);
PendingIntent pendingIntent = getPendingIntent(remoteMessage);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, AppStrings.MSG.name())
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setVibrate(null)
.setContentTitle(remoteMessage.getData().get(AppStrings.MSG.name()))
.setContentText(remoteMessage.getData().get(AppStrings.MSG.name()))
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
Notification n = mBuilder.build();
manager.notify(1, n);
private PendingIntent getPendingIntent(RemoteMessage remoteMessage)
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra(AppStrings.Notifications_Data.name(), remoteMessage.getData().get(AppStrings.Notifications_Data.name()));
intent.putExtra(AppStrings.Is_There_Notification.name(), AppStrings.YES.name());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(AppStrings.Notifications_Destination.name(), remoteMessage.getData().get(AppStrings.Notifications_Destination.name()));
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
@Override
public void onNewToken(String token)
super.onNewToken(token);
System.out.println("log Token is " + token);
Log.d("Test", "token: " + token);
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SET_ALARM " />
<uses-permission android:name="android.permission.READ_PHONE_STATE " />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS " />
<uses-permission android:name="android.permission.READ_SMS " />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".Config.App"
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"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".LocationActivity"></activity>
<activity android:name=".MapsActivity" />
<!-- <activity android:name=".ProfileActivity" />-->
<activity android:name=".ChatManagerActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".MainActivity" />
<activity android:name=".AuthActivity" />
<activity android:name=".ChatActivity" />
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name=".LoginActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver
android:name=".broadcastRecivers.AlertReciver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".broadcastRecivers.LoginReciver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".Config.FirebasePushNotificationConfig"
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.android.geo.API_KEY"
android:value="key" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>`
我发送的数据是这样的
"to":"key"
,"notification":
"title":"hello it's test"
,"body":"still a test"
,
"data":
"Notifications_Destination" : "direction"
,"MSG":"this is a test notifiation",
"Notifications_Data":5
【问题讨论】:
【参考方案1】:使用扩展 FirebaseMessagingService 创建 FcmHelper 类。
<!--Fcm Starts-->
<service
android:name="google_classes.FcmHelper"
android:exported="false">
<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/ic_noti" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/black" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="true" />
<!--Fcm End-->
【讨论】:
仍然没有运气,但现在当我重新启动应用程序时,我会收到所有在应用程序关闭时未显示的通知【参考方案2】:试试这个:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
并在应用标签下添加:
<service android:name=".FirebaseService"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
根据要求更改类名。
【讨论】:
不,仍然无法使用我在其他设备上尝试过它可以在其他所有设备上使用,但我的个人手机仍然无法使用以上是关于应用程序关闭时 Firebase 推送通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序关闭时,Rest Api Firebase 通知在 Swift 4 中不起作用