Ios 如果应用程序已在运行,如何处理推送通知?
如果应用程序已经在运行,我们如何处理推送通知?如果应用程序正在运行,我想显示一个警报(而不是推送通知警报)。仅当应用程序未运行时,才会显示推送通知警报 此外,如果我向APNs发送有效负载,如何使用“取消”按钮创建警报?您可以实施 下面是一个可能的示例代码:Ios 如果应用程序已在运行,如何处理推送通知?,ios,objective-c,push-notification,apple-push-notifications,Ios,Objective C,Push Notification,Apple Push Notifications,如果应用程序已经在运行,我们如何处理推送通知?如果应用程序正在运行,我想显示一个警报(而不是推送通知警报)。仅当应用程序未运行时,才会显示推送通知警报 此外,如果我向APNs发送有效负载,如何使用“取消”按钮创建警报?您可以实施 下面是一个可能的示例代码: - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSString *m
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]]) {
message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:@"body"];
}
if (alert) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"AThe message." delegate:self
cancelButtonTitle:@"button 1"
otherButtonTitles:@"button", nil];
[alertView show];
[alertView release];
}
“alert”键不会直接位于userInfo字典下,您需要获取另一个名为“aps”的字典,然后从“aps”字典中获取“alert”或“body”。您可以检查UIApplication的状态。就这样检查一下
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease];
[alert setTag: 2];
[alert show];
}
else {
// Push Notification received in the background
}
}
迭代3个级别的有效负载
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
NSString *message = nil;
NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ];
for (id key1 in aps){
NSLog(@"key1: %@", key1);
id alert = [aps objectForKey:key1];
if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:@"body"];
NSLog(@"body: %@, value: %@", key1, message);
message = [alert objectForKey:@"loc-args"];
NSLog(@"loc-args: %@, value: %@", key1, message);
NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ;
for (id key2 in args){
NSLog(@"key2: %@, value: ", key2);
}
message = [alert objectForKey:@"action-loc-key"];
NSLog(@"action-loc-key: %@, value: %@", key1, message);
}
else if ([alert isKindOfClass:[NSArray class]]) {
for (id key2 in key1){
NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]);
}
}
else if([key1 isKindOfClass:[NSString class]]) {
message = [aps objectForKey:key1];
NSLog(@"key1: %@, value: %@", key1, message);
}
}
}
}
结果是:
2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: {
alert = {
"action-loc-key" = Open;
body = test;
"loc-args" = (
1000,
2000
);
};
badge = 0;
"content-available" = 10;
sound = default;
}
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000,
2000
)
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value:
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value:
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10
非常感谢。苹果对此真的很不方便。@DGund真的吗?如果您试图编写Android的push(GCM/C2DM),您会发现它要复杂得多。@notnop代码有问题。应将
if(警报)
替换为if(消息)
请参阅TomTom关于检查状态的回答。这将是更好的,非黑客方式这样做。只是为了他人的利益而分享信息。yourMessage
没有定义。您至少应该说明如何从userInfo
@ShivanRaptor获取消息-我不认为这是问题的重点?这是一种更好的方法,而不是像检查userInfo是字符串还是字典这样的黑客行为。答案很好,检查过的一个不起作用,不知道为什么它被认为是好的一个而不是这个。