收到通知时 react-native-notifications 使应用程序崩溃

时间:2021-02-18 03:14:35

标签: android firebase react-native react-native-notifications

我正在使用 react-native-notifications 库通过消息云 Firebase 在我的应用程序中实现通知,我已遵循文档中的所有指南,
For react-native-notifications library
For firebase cloud messaging

因此,当我去找 firebase 顾问并发送测试通知消息时,应用程序刚刚关闭或可能会崩溃,但没有发生任何事情,我添加了 firebase-analytics 以帮助我调试问题所在以及我得到的结果当我发送测试通知消息时:

  Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/util/zzq;
       at com.google.android.gms.gcm.GcmReceiver.onReceive()
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:3177)
       at android.app.ActivityThread.-wrap18(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:156)
       at android.app.ActivityThread.main(ActivityThread.java:6523)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

这是我的代码:

import React from 'react'
import { Platform, View } from 'react-native'
import { Notifications } from 'react-native-notifications'




export default class PushNotificationManager extends React.Component {
    componentDidMount() {
        this.registerDevice()
        this.registerNotificationEvents()
    }

    registerDevice = () => {
        Notifications.events().registerRemoteNotificationsRegistered(event => {
            // TODO: Send the token to my server so it could send back push notifications...
            

            console.log('Device Token Received', event.deviceToken)
        })
        Notifications.events().registerRemoteNotificationsRegistrationFailed(event => {
            console.error(event)
        })

        Notifications.registerRemoteNotifications()
    }

    registerNotificationEvents = () => {
        Notifications.events().registerNotificationReceivedForeground((notification, completion) => {
            console.log('Notification Received - Foreground', notification)
           
            })
            // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
            completion({ alert: false, sound: false, badge: false })
        })

        Notifications.events().registerNotificationOpened((notification, completion) => {
            console.log('Notification opened by device user', notification)
           
            console.log(`Notification opened with an action identifier: ${notification.identifier}`)
            completion()
        })

        Notifications.events().registerNotificationReceivedBackground((notification, completion) => {
            console.log('Notification Received - Background', notification)
            
            // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
            completion({ alert: true, sound: true, badge: false })
        })

        Notifications.getInitialNotification()
            .then(notification => {
                console.log('Initial notification was:', notification || 'N/A')
            })
            .catch(err => console.error('getInitialNotifiation() failed', err))
    }

    render() {
        const { children } = this.props
        return <View style={{ flex: 1 }}>{children}</View>
    }
}

console.log 中它安慰令牌并在

Notifications.getInitialNotification()
                .then(notification => {
                    console.log('Initial notification was:', notification || 'N/A')
                })

它一直在安慰'N/A'

我用整个应用程序根包裹了之前的代码:

<PaperProvider theme={theme}>
            <AuthContext.Provider value={authContext}>
  /* here*/     <PushNotificationManager>
                    <NavigationContainer theme={theme}>
                        
                      <RootStackScreen />
                
                    </NavigationContainer>
/* here*/        </PushNotificationManager>
            </AuthContext.Provider>
        </PaperProvider>

0 个答案:

没有答案