2

I've set up react-native-onesignal on my project to implement Push Notification using OneSignal.

It is possible to test Push Notification on iOS simulators since Xcode 11.4 Beta. I created JSON formatted apns file to test Push Notification on Simulator and it worked very well.

But how can I emulate OneSignal Push Notification?

I followed OneSignal Documentation and want to receive Push Notification which is sent from OneSignal Dashboard.

Here's what I've implemented on my App.tsx file.

const initializeOneSignal = () => {
  OneSignal.setLogLevel(6, 0);

  OneSignal.init("MY_ONESIGNAL_APP_ID", {
    kOSSettingsKeyAutoPrompt: false,
    kOSSettingsKeyInAppLaunchURL: false,
    kOSSettingsKeyInFocusDisplayOption: 2,
  });
  OneSignal.inFocusDisplaying(2);

  OneSignal.promptForPushNotificationsWithUserResponse(myiOSPromptCallback);

  OneSignal.addEventListener('received', onPNReceived);
  OneSignal.addEventListener('opened', onPNOpened);
  OneSignal.addEventListener('ids', onPNIds);
};

useEffect(() => {
  initializeOneSignal();

  return () => {
    OneSignal.removeEventListener('received', onPNReceived);
    OneSignal.removeEventListener('opened', onPNOpened);
    OneSignal.removeEventListener('ids', onPNIds);
  };
}, []);

const onPNReceived = notification => {
  console.log('Notification received: ', notification);
};

const onPNOpened = openResult => {
  console.log('Message: ', openResult.notification.payload.body);
  console.log('Data: ', openResult.notification.payload.additionalData);
  console.log('isActive: ', openResult.notification.isAppInFocus);
  console.log('openResult: ', openResult);
};

const onPNIds = device => {
  console.log('Device info: ', device);
};

const myiOSPromptCallback = permissions => {
  console.log('Permissions: ', permissions);
};

I cannot see any logged message when I sent Push Notification from my OneSignal Dashboard.

Do I need to do any trick in apns file?

Any help would be appreciated.

michael
  • 4,053
  • 2
  • 12
  • 31

2 Answers2

1

You can check out this article on Medium enter link description here.

This article helped me test oneSignal push notifications on my iOS simulator and figure out the deep linking issue.

Here are my steps:

  1. Create a payload.apns file on your desktop
  2. Put the following code inside it:
{
    "Simulator Target Bundle": "com.example.app",
    "aps": {
        "alert": {
            "title": "Push Notification",
            "subtitle": "Test Push Notifications",
            "body": "Testing Push Notifications on iOS Simulator",
        }
    },
    "custom": {
        "i": "notificationId as UUID",
        "a": {"deeplinkKey": "{\"deeplinkDetailKey\":\"deeplinkDetailValue\"}", "launchURL": "example://collection/myCollectionId/type/1"}
    }
}
  1. Remember to replace com.example.app with your app bundle ID.
  2. Run your app on the simulator, then close the app and keep metro open
  3. Drag and drop payload.apns file on the simulator, you will see the notification to click on it. If you have done the 3rd step right, you should be redirected to your app
  4. (Optional) If you need to test a deep link with the push notification, I haven't yet found the key oneSignal uses, but as you see in the .apns file above, I added a launchURL to the a which represents additionalData and handled catching and opening it manually in the app to test what's wrong with the deep link.
  5. (Optional) To open a deep link in your app, you can use Linking API from react-native:
import {Linking} from 'react-native'

Linking.openURL("Path/to/launchURL")
Mahdieh Shavandi
  • 4,906
  • 32
  • 41
0

tl;dr you can't send a real notification to a simulator. You can only send mocked notifications to a simulator

Your server is oblivious of a simulator, because simulators don't have device tokens. Apple decided not to give it one. I suppose this was so users can't fake their device token and get notifications on their simulator...

The 11.4 simply allows the drag and drop of a APNs payload into the simulator without any mention of device token.

mfaani
  • 33,269
  • 19
  • 164
  • 293