react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
749 stars 286 forks source link

setBackgroundMessageHandler , didReceiveRemoteNotification not working For IOS , Please Help ! #165

Open mertbuldur opened 4 years ago

mertbuldur commented 4 years ago

hello I am trying to catch notifications when the application is closed or at the bottom. Notifications on the front work, but notifications on the back. I am trying to capture with setBackgroundMessageHandler, but I cannot receive a message in the log or console. I run it once but I don't remember the codes Please help me I have been dealing with it for 2 days.

AppDelegate.m `#import "AppDelegate.h"

import

import <UserNotifications/UserNotifications.h>

import <React/RCTBridge.h>

import <React/RCTBundleURLProvider.h>

import <React/RCTRootView.h>

// Firebase @import Firebase;

@implementation AppDelegate

// Push Nofitication // Required to register for notifications

// Required for the registrationError event.

-(void)userNotificationCenter:(UNUserNotificationCenter )center willPresentNotification:(UNNotification )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

NSLog(@"APP_PUSH from foreground %@ ",completionHandler); NSLog(@"APP_PUSH from foreground2 %@ ",completionHandler);

completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);

}

// Sistem

@end `

Package.json: "@react-native-firebase/app": "6.7.1", "@react-native-firebase/messaging": "6.7.1", @react-native-community/push-notification-ios": "^1.2.0", "react-native-push-notification": "^3.1.9", App.js ` messaging().setBackgroundMessageHandler( remoteMessage => { PushNotification.localNotification({ title: "xxxx", message: "testxxxting", // (required),

        });
        console.log('Message handled in the background!', remoteMessage);
    });

PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android)

onRegister: function (token) {
    console.log("TOKEN:", token);
},

// (required) Called when a remote or local notification is opened or received
onNotification: function (notification) {

    console.log("NOTIFICATION:", notification);

    // process the notification

    // required on iOS only (see fetchCompletionHandler docs: https://github.com/react-native-community/react-native-push-notification-ios)
    if (!notification.foreground) {
        notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
},

// ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: "503601710565",

// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
    alert: true,
    badge: true,
    sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,

/**
 * (optional) default: true
 * - Specified if permissions (ios) and token (android and ios) will requested or not,
 * - if not, you must call PushNotificationsHandler.requestPermissions() later
 */
requestPermissions: true

});

`

axeljeremy7 commented 4 years ago

hey did you find a solution?

I am unable to get token with getAPNSToken() but with react-native-push-notification I am able to get token and work for background and foreground. But I need to get the token without the need of react-native-push-notification. In android it works fine.

    try {

        let tries = 0;
        let apnsToken = null;
        while (tries < 5) {
            const apnsToken = await messaging().getAPNSToken();
            if (apnsToken !== null) return apnsToken;
            await new Promise((resolve) => setTimeout(resolve, 1000));
            tries++;
        }
        // await syncStorage.set('firebaseToken', apnsToken);
        // report({firebaseToken: syncStorage.get('firebaseToken')}, 'firebaseToken IOS');
        report(apnsToken, 'getAPNSToken()');
        return apnsToken;
    } catch (e) {
        reportErrorInstabug(e, 'registerAppWithAPN()', 'ERROR');
        return null;
    }
}