【问题标题】:iOS) Push Notification using parse when app is backgroundiOS)当应用程序为后台时使用解析推送通知
【发布时间】:2015-12-08 17:38:19
【问题描述】:

我针对同一个问题查看了很多问题和答案。

而我现在知道的是

  1. 当应用处于后台时不会调用“didReceiveRemoteNotification”。
  2. didReceiveRemoteNotification 仅在应用程序处于前台时调用,或者当用户通过点击通知进入应用程序(如果它在后台)时调用。
  3. application:didFinishLaunchingWithOptions: 当用户点击通知以打开应用程序时调用它。

我的情况如下:

  1. 我正在使用解析来推送和获取通知。
  2. 当应用程序处于前台并且我正在处理数据并在通知列表中显示警报和列表通知时,我成功获得了通知。
  3. 但当应用程序处于后台或被终止时,我无法收到通知。
  4. 但是我检查了我的应用程序是否可以在我的应用程序处于后台或在使用 Parse 的“自定义受众”时根据需要被终止时收到通知。 (与其他著名应用程序一样)

我想在应用程序处于后台或被杀死时收到通知,就像我使用 Parse 的“自定义受众”时一样。 但是当我只使用 Parse 的 API 时,它不能按我的意愿工作。

我现在有什么遗漏吗?

我的注册码如下:

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

【问题讨论】:

  • 请添加您如何注册通知的代码。如果您在前台收到通知,那么您也必须在后台收到通知,请检查通知托盘。
  • @Milan 我做到了,米兰请帮忙
  • 您的 iOS 代码对于在 Parse 上注册设备令牌是正确的。当应用程序进入后台时,您是否清除了令牌?另外,您如何从 Parse 发送通知?因为您在前台而不是在后台收到通知真的很奇怪。尝试检查通知托盘并在推送控制台上查看通知是否已传递。
  • @Milan 正如我上面所说,我认为您的问题毫无意义,因为当我使用自定义解析受众而不是 pase api 时,我会在应用程序处于后台或被杀时收到推送通知
  • 我问这些问题只是为了帮助你,因为你没有提供太多信息,但如果你有这种态度,我很抱歉我帮不上忙。

标签: ios parse-platform push-notification apple-push-notifications


【解决方案1】:

使用方法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
}

official documentation 表示如下:

用户点击警报中的默认按钮或点击(或单击) 应用程序图标。如果点击默认操作按钮(在正在运行的设备上 iOS),系统启动应用程序,应用程序调用其委托的 application:didFinishLaunchingWithOptions: 方法,传入 通知负载(用于远程通知)或 local-notification 对象(用于本地通知)。虽然 application:didFinishLaunchingWithOptions: 不是最好的地方 处理通知,此时获取有效负载会给你 在处理程序方法之前启动更新过程的机会 被调用。

【讨论】:

  • 我不是在谈论如何处理来自tapp的用户,我是在谈论如何在应用处于后台时获取和显示通知
  • 当远程通知到达并且应用程序在后台时,它们会直接显示在设备上。您无法在后台接收通知并向用户显示自定义内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多