我在处理使用 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");
}
.....
est 这个响应,但我无法处理推送消息:
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
有什么帮助吗?谢谢。
最佳答案
当应用程序未运行或在后台时,我无法处理通知,我改变了分辨率的焦点,我正在像 android 应用程序那样做,但我 iOS 用“消息控制”处理消息,我没有找到处理的“方法”这。
我开始使用 channel 订阅消息的推送通知取决于用户设置。我通过 channel 和应用订阅 channel 发送到 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 中的 channel :
- (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];
}
您可以在任何地方更改 channel ,如下所示:
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.channels = @[@"channel3"];
[currentInstallation saveInBackground];
也许这可以帮助某人。
关于ios - 当应用程序未运行时处理 JSON 推送通知解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043633/