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

Handle JSON push notification parse when app is not running

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

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

但是当应用程序在后台或被杀死时,此消息将被处理并直接发送到通知栏。我试图在这个函数中处理:

- (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

有什么帮助吗?谢谢

将您的代码添加到此函数,当应用程序被终止并且您收到推送通知时 - 它会调用此函数,以便您可以根据此通知重新启动您的应用程序并相应地在应用程序中显示警报。

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

当应用程序不在 运行 或在后台时我无法处理通知,我改变了分辨率的焦点,我做的就像 android 应用程序的但我 iOS 处理消息使用 "Message Control",但我没有找到 "the way" 来处理这个问题。

我开始使用 频道 的推送通知订阅消息,具体取决于用户设置。我发送带有频道和应用程序订阅频道的 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];

也许这可以帮助某人。