Firebase Cloud Messaging 推送通知在 Android 上无法在前台工作,只能在后台工作
Firebase Cloud Messaging push notification not working in foreground, only background, on Android
我正在尝试创建一个可以在满足某些要求时接收通知的应用程序。我试图建立一个单独的项目来制作一个简单的云消息系统,但是当应用程序在前台 运行 时没有通知显示,仅在后台显示。
这是主要的 MapActivity 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent intentBackgroundService = new Intent(this,FirebasePushNotification.class);
startService(intentBackgroundService);
}
Firebase 消息传递 Class:
package com.example.traindetectionproject;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebasePushNotification extends FirebaseMessagingService {
public FirebasePushNotification() {
super();
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.traindetectionproject">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the "MyLocation" functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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/Theme.Traindetectionproject">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebasePushNotification">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
</intent-filter>
</service>
</application>
</manifest>
感谢您的任何建议
在前台,通知在 onMessageReceived
中收到但未显示,那么您在 onMessageReceived
中的代码应该编写并显示它,
相反,当应用程序在后台时,通知会直接显示在通知栏中,而不会 onMessageReceived
被调用
看这里:https://firebase.google.com/docs/cloud-messaging/android/receive
下面的代码片段可能会帮助您找到解决方案
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken ( @NonNull String s ) {
super.onNewToken ( s );
}
@Override
public void onMessageReceived ( @NonNull RemoteMessage remoteMessage ) {
super.onMessageReceived ( remoteMessage );
NotificationManager notificationManager = (NotificationManager) getSystemService ( Context.NOTIFICATION_SERVICE );
if (!Objects.equals ( null, remoteMessage.getNotification () )) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel ( NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH );
notificationManager.createNotificationChannel ( notificationChannel );
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder ( this, NOTIFICATION_CHANNEL_ID );
notificationBuilder.setAutoCancel ( true )
.setStyle ( new NotificationCompat.BigTextStyle ().bigText ( remoteMessage.getNotification ().getBody () ) )
.setDefaults ( Notification.DEFAULT_ALL )
.setWhen ( System.currentTimeMillis () )
.setSmallIcon ( R.drawable.ic_notification_icon )
.setTicker ( remoteMessage.getNotification ().getTitle () )
.setPriority ( Notification.PRIORITY_MAX )
.setContentTitle ( remoteMessage.getNotification ().getTitle () )
.setContentText ( remoteMessage.getNotification ().getBody () );
notificationManager.notify ( 1, notificationBuilder.build () );
}
}
}
将此添加到您的 AndroidManifest.xml 任何 Activity TAG
之前
<service
android:name=".utilities.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我正在尝试创建一个可以在满足某些要求时接收通知的应用程序。我试图建立一个单独的项目来制作一个简单的云消息系统,但是当应用程序在前台 运行 时没有通知显示,仅在后台显示。
这是主要的 MapActivity 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent intentBackgroundService = new Intent(this,FirebasePushNotification.class);
startService(intentBackgroundService);
}
Firebase 消息传递 Class:
package com.example.traindetectionproject;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebasePushNotification extends FirebaseMessagingService {
public FirebasePushNotification() {
super();
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.traindetectionproject">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the "MyLocation" functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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/Theme.Traindetectionproject">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebasePushNotification">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
</intent-filter>
</service>
</application>
</manifest>
感谢您的任何建议
在前台,通知在 onMessageReceived
中收到但未显示,那么您在 onMessageReceived
中的代码应该编写并显示它,
相反,当应用程序在后台时,通知会直接显示在通知栏中,而不会 onMessageReceived
被调用
看这里:https://firebase.google.com/docs/cloud-messaging/android/receive
下面的代码片段可能会帮助您找到解决方案
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken ( @NonNull String s ) {
super.onNewToken ( s );
}
@Override
public void onMessageReceived ( @NonNull RemoteMessage remoteMessage ) {
super.onMessageReceived ( remoteMessage );
NotificationManager notificationManager = (NotificationManager) getSystemService ( Context.NOTIFICATION_SERVICE );
if (!Objects.equals ( null, remoteMessage.getNotification () )) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel ( NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH );
notificationManager.createNotificationChannel ( notificationChannel );
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder ( this, NOTIFICATION_CHANNEL_ID );
notificationBuilder.setAutoCancel ( true )
.setStyle ( new NotificationCompat.BigTextStyle ().bigText ( remoteMessage.getNotification ().getBody () ) )
.setDefaults ( Notification.DEFAULT_ALL )
.setWhen ( System.currentTimeMillis () )
.setSmallIcon ( R.drawable.ic_notification_icon )
.setTicker ( remoteMessage.getNotification ().getTitle () )
.setPriority ( Notification.PRIORITY_MAX )
.setContentTitle ( remoteMessage.getNotification ().getTitle () )
.setContentText ( remoteMessage.getNotification ().getBody () );
notificationManager.notify ( 1, notificationBuilder.build () );
}
}
}
将此添加到您的 AndroidManifest.xml 任何 Activity TAG
之前<service
android:name=".utilities.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>