简体   繁体   中英

iOS) Push Notification using parse when app is background

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];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM