React-native-onesignal: Permission asked automatically when the app starts

Created on 9 Nov 2016  路  12Comments  路  Source: OneSignal/react-native-onesignal

For some reason the popup asking permission for the push notifications is showing automatically when the app is opened for the first time even if I'm not calling OneSignal anywhere inside my JS code.
Is this the expected behaviour?
I'd like to be able to ask for permission inside the app at some point, not on the first loading (which is very bad).

Package version:

"react-native": "0.37.0",
"react-native-onesignal": "^1.2.3",

app.js

import React from 'react';
import { Text } from 'react-native';
// import OneSignal from 'react-native-onesignal';

// const pendingNotifications = [];

// const handleNotification = (notification) => {
//   console.log(pendingNotifications);
//   alert(notification.message);
// };

// OneSignal.configure({
//   onIdsAvailable(device) {
//     console.log('UserId = ', device.userId);
//     console.log('PushToken = ', device.pushToken);
//   },
//   onNotificationOpened(message, data, isActive) {
//     const notification = {
//       message,
//       data,
//       isActive,
//     };
//     pendingNotifications.push(notification);
//     handleNotification(notification);
//   },
//   enableInAppAlertNotification: true,
// });
const WelcomeScreen = () => <Text>Hello world</Text>;
export default WelcomeScreen;

Most helpful comment

@mtt87 in your AppDelegate.m: add "autoRegister: false":

self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions appId:@"<app id>" autoRegister: false];

then in your js when you want to ask permission:

OneSignal.registerForPushNotifications();

All 12 comments

Anyone here can give me some hint to where to start debugging? (given the fact that I'm not iOS dev but JS dev, I'm not much familiar with Xcode/Swift/Obj-c)

Thank you

@mtt87 in your AppDelegate.m: add "autoRegister: false":

self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions appId:@"<app id>" autoRegister: false];

then in your js when you want to ask permission:

OneSignal.registerForPushNotifications();

@arshavinho seems like it worked, the permission is now asked when I trigger
OneSignal.registerForPushNotifications();

I do a double check and see if the notifications are working fine and then I think I can add this to README since it's never mentioned.

Confirmed! Works like a charm adding autoRegister: false! 馃憤

They have changed the API and now you have to pass a dictionary instead:

  self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions
                                                         appId:@"your-app-id"
                                                      settings:@{kOSSettingsKeyAutoPrompt : @NO}];

@goldengecko When I'm adding this line of code to AppDelegate.m, I'm getting this Xcode error: "Property 'oneSignal' not found on object of type 'AppDelegate *'". What should I do?

Here is how my AppDelegate.m file look like (I have ExpoKit project):

#import "AppDelegate.h"
#import <React/RCTPushNotificationManager.h>


@implementation AppDelegate

// Put your app delegate methods here. Remember to also call methods from EXStandaloneAppDelegate superclass
// in order to keep Expo working. See example below.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions
                                                           appId:@"your-app-id"
                                                        settings:@{kOSSettingsKeyAutoPrompt : @NO}];

    return [super application:application didFinishLaunchingWithOptions:launchOptions];

}

@end

You haven't declared that variable anywhere. Suggest @synthesize oneSignal = _oneSignal;

@goldengecko If I'm adding @synthesize oneSignal = _oneSignal; I'm getting an errorProperty implementation must have its declaration in interface 'AppDelegate' or one of its extensions

Yes, you would need to declare it in the header @property (strong, nonatomic) RCTOneSignal* oneSignal; and have the synthesize in the implementation. Just the normal style of variable handling

any suggestion for Android SDK i am facing the same.

I am not using OneSignal but still getting the same issue, Any idea how to solve?

Was this page helpful?
0 / 5 - 0 ratings