Search code examples
iospush-notificationapple-push-notificationsonesignal

OneSignal Not calling didReceiveRemoteNotification


We migrated from UA to One Signal. We are sending push from cloud code like

var pushInfo = {
      "app_id" : "xxxxxx",          
      "data": {
          "objectId": objectId,
          "placeId": placeId,
      },
      "included_segments": ["All Users"],
      "contents": {"en": message}
};
var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic XXXXX"
};

var options = {
 host: "onesignal.com",
 port: 443,
 path: "/api/v1/notifications",
 method: "POST",
 headers: headers,
};

var https = require('https');
var req = https.request(options, function(res) {  
res.on('data', function(data) {
  console.log("Response:");
  console.log(JSON.parse(data));
});
});

req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
 });  
req.write(JSON.stringify(pushInfo));
req.end();

In my AppDelegate.m I do

[OneSignal initWithLaunchOptions:launchOptions appId:@"XXXXX"];

Now earlier when a notification is received and user Taps on it, it used to call

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

Q. This is not getting called now. How do I handle it with OneSignal. Q. What I need to do to handle a silent notification (no visible badge/banner etc)


Solution

  • I Assume you are testing/running your app on an iOS10 device,

    I looked at OneSignal SDK Code and I think the SDK automatically uses the new UserNotifications Framework (add in iOS10), when iOS10 is detected on device.

    In this case, the AppDelegate method you mentioned above does not get invoked, instead methods in UNUserNotificationCenterDelegate get invoked, which are captured by SDK to record clicks/views.

    To Fix the issue, Create a new class which implements OSUserNotificationCenterDelegate and provide its instance to OneSignal using [OneSignal setNotificationCenterDelegate:yourCustomNotificationCenterDelegateInstance]

    Please note that application:didReceiveRemoteNotification:fetchCompletionHandler: is still called when a silent push notification (content-available: 1) arrives, but its not called when user taps the notification if UNUserNotificationCenterDelegate is used.

    Also, there was an issue on iOS 10.0.X where the application:didReceiveRemoteNotification was called instead of application:didReceiveRemoteNotification:fetchCompletionHandler: See: https://forums.developer.apple.com/thread/54332 , but I doubt if this is the case with you.