在前台

时间:2018-02-02 09:48:52

标签: android react-native react-native-android android-push-notification react-native-fcm

应用程序在后台接收推送通知时工作正常,但在foregrond时它会崩溃。我正在使用RN 0.49和react-native-fcm 11.3.1包。

我的代码如下所示:

    FCM.on(FCMEvent.Notification, async (notif) => {
    // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
    if(notif.local_notification){
        // return;
        console.log("1111");
        this.showLocalNotification(notif);
    }
    if(notif.opened_from_tray){
      //iOS: app is open/resumed because user clicked banner
      //Android: app is open/resumed because user clicked banner or tapped app icon
      //   return;
        console.log("2222");
        this.showLocalNotification(notif);

    }
    // await someAsyncCall();

    if(Platform.OS ==='ios'){
      //optional
      //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application.
      //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
      //notif._notificationType is available for iOS platfrom
      switch(notif._notificationType){
        case NotificationType.Remote:
          notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
          break;
        case NotificationType.NotificationResponse:
          notif.finish();
          break;
        case NotificationType.WillPresent:
          notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
          break;
      }
    }
});

FCM.on(FCMEvent.RefreshToken, (token) => {
    console.log("---------Refresh Token-------------")
    console.log(token)
    // fcm token may not be available on first load, catch it here
});

showLocalNotification(notif) {
        FCM.presentLocalNotification({
            title: notif.title,
            body: notif.body,
            priority: "high",
            click_action: notif.click_action,
            show_in_foreground: true,
            local: true
        });
    }

componentDidMount(){
        // iOS: show permission prompt for the first call. later just check permission in user settings
        // Android: check permission in user settings
        FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));

        FCM.getFCMToken().then(token => {
            console.log(token)
            // store fcm token in your server
        });

        this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
            console.warn("listening.......................");
        });

        // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
        // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
        // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
        FCM.getInitialNotification().then(notif => {
           console.log(notif)
        });
    }

MainApplication.java

    public class MainApplication extends MultiDexApplication {

  // Needed for `react-native link`
  public List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        // Add your own packages here!
        // TODO: add cool native modules
//            new MainReactPackage(),

            new RNBackgroundGeolocation(),
            // Needed for `react-native link`

            new FIRMessagingPackage(),
            //new RNBackgroundGeolocation(),
            //new RNFirebasePackage(),
            new VectorIconsPackage()
            //new RNFirebaseMessagingPackage()
    );
  }
}

Android Studio出现此错误:

  

java.lang.ClassCastException:x.y.z.MainApplication   无法转换为com.facebook.react.ReactApplication                                                                              在   com.evollu.react.fcm.MessagingService $ 1.run(MessagingService.java:41)                                                                              在android.os.Handler.handleCallback(Handler.java:751)                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                              在android.os.Looper.loop(Looper.java:154)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6682)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我不知道是什么导致应用程序在应用程序打开时仅在前台状态崩溃! 关于如何解决这个问题的任何想法?

0 个答案:

没有答案