0
votes

I looked a lot questions and answers for the same issue.

And What I know now is that

  1. 'didReceiveRemoteNotification' is not called when the app is background.
  2. didReceiveRemoteNotification is just called when app is foreground or when user comes to app by tapping notification if its in background.
  3. application:didFinishLaunchingWithOptions: is called when user taps on notification to open the app if its killed.

My situation is below:

  1. I'm using parse to push and get notification.
  2. I'm successful getting notification when app is foreground and I'm handling data and show alert and list notification at notification list.
  3. But I can not get notification when app is background or killed.
  4. But I check my app can get notification when my app is background or killed when using 'custom audience' of Parse as I want. (Like other famous applications)

What I want to is I want to get notification when app is background or killed like when I use 'custom audience' of Parse. But when I use just API of Parse , it doesn't work as i want.

Is there anything I'm missing now?

My registering code is below:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"didReceiveRemoteNotification executed : %@",userInfo);
NSInteger bettingIDX = [[userInfo objectForKey:@"betting_idx"] integerValue];
NSString *message = [userInfo objectForKey:@"message"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:@"%@",message];
NSUUID *uuid = [NSUUID UUID];
NSString *notificationKey = [uuid UUIDString];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"DeviceToken : %@", deviceToken );
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

[Parse setApplicationId:@"applicationID"
              clientKey:@"clientID"];
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
1
Please add the code how are you registering for the notifications. If you are getting notification in Foreground then you must get it in background as well, check the Notification Tray.Milan Agarwal
@Milan I did it milan please helpuser2649058
Your iOS code is correct for registering the device token on Parse. Are you clearing the token when the app goes background ? Also how do you send notification from Parse ? As it's really strange that you are getting notification in foreground and not in background. Try checking the notification tray also look on the Push console that whether the notification is delivered or not.Milan Agarwal
@Milan As I said above, i think your question is meaningless because I get push notification when app is background or killed as i want when i use custom audience of parse, not api of paseuser2649058
I am asking these questions just to help you out as you have not provided too much information, but if you have this kind of attitude then I am sorry I can't help.Milan Agarwal

1 Answers

0
votes

Use method didFinishLaunchingWithOptions:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    if let launchOptions = launchOptions as? [String: AnyObject],
        let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: String] {

    }

    return true
}

The official documentation says the following:

The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.