You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Firebase应用关闭/划除时推送通知配置问题

React Native应用关闭时展示推送通知(react-native-firebase配置)

我想要实现应用关闭时仍能展示推送通知的功能,目前正在通过react-native-firebase来配置推送通知,已经按照官方文档的要求完成了AndroidManifest.xml的基础设置,当前的配置内容如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.moonsite.sqlivetrivia" 
          android:versionCode="1" 
          android:versionName="1.0"> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <!-- 此处省略其他配置内容 -->
</manifest>

为了确保应用关闭时能正常接收并展示推送通知,还需要补充以下关键配置和步骤:

  • 添加必要的权限:除了已有的权限,还需要添加推送相关的核心权限,保证系统能唤醒应用处理通知:

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
  • 配置Firebase消息服务:在AndroidManifest.xml的application标签内注册自定义的消息处理服务,确保应用在后台或关闭状态下能响应推送事件:

    <application ...>
      <!-- 前台/后台消息接收服务 -->
      <service
          android:name=".MyFirebaseMessagingService"
          android:exported="false">
          <intent-filter>
              <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
      </service>
    
      <!-- Android 12+ 后台消息处理服务 -->
      <service
          android:name=".MyFirebaseBackgroundMessagingService"
          android:exported="false"
          android:permission="android.permission.BIND_JOB_SERVICE">
          <intent-filter>
              <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
      </service>
    </application>
    
  • 实现消息处理逻辑:在自定义的MyFirebaseMessagingService中重写onMessageReceived方法,手动构建并展示通知——即使应用处于关闭状态,系统也会触发这个方法处理推送:

    // MyFirebaseMessagingService.java
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    import android.app.NotificationManager;
    import android.content.Context;
    import androidx.core.app.NotificationCompat;
    
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            
            // 从推送 payload 中获取通知内容
            String title = remoteMessage.getNotification() != null ? remoteMessage.getNotification().getTitle() : "新通知";
            String content = remoteMessage.getNotification() != null ? remoteMessage.getNotification().getBody() : "您有一条新消息";
    
            // 构建通知
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "push_notification_channel")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
            // 展示通知
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify((int) System.currentTimeMillis(), builder.build());
        }
    }
    
  • 创建通知渠道(Android 8.0+):Android 8.0及以上版本要求必须创建通知渠道才能展示通知,建议在应用启动时完成初始化:

    // 在Application类或主Activity的onCreate方法中添加
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        android.app.NotificationChannel channel = new android.app.NotificationChannel(
                "push_notification_channel",
                "推送通知",
                NotificationManager.IMPORTANCE_DEFAULT
        );
        notificationManager.createNotificationChannel(channel);
    }
    

最后要注意:如果推送仅包含data类型的payload,应用关闭时系统不会自动展示通知,必须通过后台服务处理;而包含notification字段的payload,系统会直接在通知栏展示,无需额外处理。

内容的提问来源于stack exchange,提问作者Manspof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:50:00