当应用未运行时处理JSON推送通知解析

时间:2015-01-20 10:59:48

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

我在使用parse.com处理推送消息时遇到问题,当应用程序运行时,我可以处理json并使用以下内容构造消息:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

但是当app处于后台或被杀时,此消息将被处理并直接发送到通知栏。我试图处理这个功能:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
....
 UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (localNotif) {

        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99];
        UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"
                                                       message:@"app was INACTIVE"
                                                      delegate:self
                                             cancelButtonTitle:@"a-ha!"
                                             otherButtonTitles:nil];
        [BOOM show];
        NSLog(@"App was NOT ACTIVE");
    }
.....

估计这个回复,但我无法处理推送消息:

Handling Push Notifications when App is NOT running

Can't handle push notifications when app is running background

Handling push notifications when app is not running (App is Killed)

how can I handle push notification when my app is not running

有任何帮助吗? THX。

2 个答案:

答案 0 :(得分:0)

当app没有运行或在后台时,我无法处理通知,我改变了解决方案的焦点,我正在做Android应用程序,但我用iOS处理消息“消息控制”,我没有找到“的方式“处理这件事。

我开始根据用户设置使用推送通知的频道订阅消息。我通过频道和app订阅频道发送给Parse消息。

您可以通过POST发送通知:

  curl -X POST \
  -H "X-Parse-Application-Id: {APP-KEY}" \
  -H "X-Parse-REST-API-Key:   {REST-API-KEY}" \
  -H "Content-Type: application/json" \
  -d '{
        "channels": [
          "valencia",
          "sevilla"
        ],
        "data": {
          "alert": "Fin del partido Betis - Valencia 0-2",
          "sound": "gol.mp3"
        }
      }' \
  https://api.parse.com/1/push

在App中,您可以订阅AppDelegate中的频道:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:newDeviceToken];

    NSArray * channels = @[@"channel1", @"channel2"];
    NSLog(@"Channels : %@", channels);
    currentInstallation.channels = channels;
    [currentInstallation saveInBackground];
}

您可以在任何地方更改频道,例如:

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.channels = @[@"channel3"];

[currentInstallation saveInBackground];

也许这可以帮助别人。

答案 1 :(得分:-1)

将代码添加到此功能,当应用程序被终止并且您获得推送通知时 - 它将调用此功能,以便您可以在此通知上重新启动应用程序并在应用程序中显示警报。

(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo