FCM 测试通知未出现在我的 Android 模拟器上

问题描述 投票:0回答:1

我正在尝试使用 Firebase Cloud Messaging 和 React Native 将测试通知从 Firebase 发送到 Android 模拟器。我在 VSC 终端中收到 FCM 注册令牌,但当我尝试发送测试通知时,我的终端或 Android 模拟器中没有显示任何内容。我已将此代码添加到我的 build.gradle(android) 文件中:

buildscript {
repositories {
...
google()  
}
dependencies {
...
classpath 'com.google.gms:google-services:4.3.8'
}
}
allprojects {
...
repositories {
...
google()  
...
}
dependencies{
....
 classpath('com.google.gms:google-services:4.4.1')
 classpath('com.google.firebase:firebase-crashlytics-gradle:2.7.0')
}
}

这是我添加到我的应用程序 build.gradle 中的代码:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
...
dependencies {

implementation platform('com.google.firebase:firebase-bom:28.2.1')
...
}

将此代码添加到我的 AndroidManifest.xml 文件中:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application .......>
<meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="YOUR NOTIFICATION CHANNEL NAME"/>
<meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
<meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@android:color/white"/>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
... </application>

这是我的配置代码:

import { Component } from "react"
import PushNotification from "react-native-push-notification"
import PushNotificationIOS from "@react-native-community/push-notification-ios"

export default class PushController extends Component {
  componentDidMount() {
    PushNotification.configure({
    
      onRegister: function (token) {
        console.log("TOKEN:", token) // I GET THE TOKEN SUCCESSFULLY
      },
      
      onNotification: function (notification) {
        console.log("NOTIFICATION:", notification) // CANNOT SEE THIS LOG

       
        notification.finish(PushNotificationIOS.FetchResult.NoData)
      },
      
      senderID: "44110657871",
     
      permissions: {
        alert: true,
        badge: true,
        sound: true,
      },
      popInitialNotification: true,
      requestPermissions: true,
    })
  }
  render() {
    return null
  }
}

这是我在 PushNotifications.js 中的代码:

import React, { Fragment } from "react"
import PushController from "./PushController"

import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  FlatList,
} from "react-native"
import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from "react-native/Libraries/NewAppScreen"
// Dummy data for list, we'll replace this with data received from push
let pushData = [
  {
    title: "First push",
    message: "First push message",
  },
  {
    title: "Second push",
    message: "Second push message",
  },
]
_renderItem = ({ item }) => (
  <View key={item.title}>
    <Text style={styles.title}>{item.title}</Text>
    <Text style={styles.message}>{item.message}</Text>
  </View>
)
const PushNotifications = () => {
  async function requestUserPermission() {
    const authStatus = await messaging().requestPermission()
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL

    if (enabled) {
      console.log("Authorization status:", authStatus)
    }
  }

  return (
    <Fragment>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}
        >
          <Header />
          <View style={styles.listHeader}>
            <Text>Push Notifications</Text>
          </View>
          <View style={styles.body}>
            <FlatList
              data={pushData}
              renderItem={(item) => this._renderItem(item)}
              keyExtractor={(item) => item.title}
            />
            {/* <LearnMoreLinks /> */}
          </View>
        </ScrollView>
      </SafeAreaView>
      <PushController />
    </Fragment>
  )
}
const styles = 
...
export default PushNotifications

请帮助我!

react-native push-notification firebase-cloud-messaging android-emulator
1个回答
0
投票

使用已安装“google API”的 Android SDK 在 Android Studio > AVD Manager 中创建一个新模拟器

此外,请确保已安装 SDK Manager > SDK Tools > Google Play Services。

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