上下观古今,起伏千万途。这篇文章主要讲述当app在前台时发送推送通知相关的知识,希望能为你提供帮助。
即使应用程序在后台,我如何发送推送通知?我有一个android应用程序,它接收来自服务器的推送通知,当应用程序在后台时它不会发送。它仅在应用程序处于后台时发送。
答案您需要在清单文件中添加:
<
service
android:name=".name_of_your_firebase_instace_service"
android:exported="false">
<
intent-filter>
<
action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
<
/intent-filter>
<
/service>
<
service
android:name=".nname_of_your_firebase_messaging_service
android:exported="false">
<
intent-filter>
<
action android:name="com.google.firebase.MESSAGING_EVENT" />
<
/intent-filter>
<
/service>
这是用于刷新令牌的类(实例服务)
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// sending reg id to your server
sendRegistrationToServer(refreshedToken);
Log.d("NewToken",refreshedToken);
}private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
} }
消息服务,您将收到消息/通知
public class MyFirebaseMessagingService extends FirebaseMessagingService {private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.// Check if message contains a data payload.
if (remoteMessage.getData().size() >
0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
//you can send your own custom notification here if you are sending notification in data tag or if you are sending notification with "notification tag" it will handle it automatically}
}
}
【当app在前台时发送推送通知】注意:不要忘记在项目的app文件夹中添加google-service.json文件
推荐阅读
- com.google.android.gms.common.internal.safe parcel.safe parcelable not found
- React app,componentDidUpdate - 跳转列表,无限循环
- Android XML解析与泰米尔新闻API
- Android Studio(加载时MapView应用崩溃)
- 为什么PKWARE网站上的ZIP APPNOTE没有在其规范中提到正确的密码检查机制()
- 在React-Native Expo的App购买中()
- 从服务器[DF-AA-20]检索信息时的应用程序内购买错误 - android
- 在收到用于自动续订订阅的Apple statusUpdateNotification时,如何确定客户/用户()
- IAP - 无法连接到iTunes Store - 仅在Apple审核期间