[英]Handling Apple Push Notification Service
我在我的项目中使用苹果推送通知服务。
请按照打开应用程序和处理此推送通知的2种方式进行操作。 在第二种情况下,我不知道如何处理它。 你知不知道怎么?
推送通知到达我的设备,
场景1:
我点击了推送通知。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
函数AppDelegate.m
文件捕获此函数。
场景2:
我通常打开设备(通过点击应用程序)
我该如何处理推送通知?
其他答案显示了当用户点击通知时如何获取通知数据。
显示的两个方法之间的区别在于,当应用程序已经在前台或后台运行时调用一个,而当应用程序根本不运行时调用另一个。
在第二种情况下,当用户未点击通知时,当您使用启动图标打开通知数据时,通知数据不会传递给应用程序。
第一种情况:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog (@"APNS: notification received: %@", userInfo);
NSString *message = nil;
id alert = [userInfo objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:@"alert"];
}
if (message)
{
if (![message isEqualToString:@""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"notification"
message: message
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
第二种情况:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog (@"LAUNCH OPTIONS: %@",launchOptions);
id remoteNotificationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotificationValue)
{
NSString *message = nil;
id alert = [remoteNotificationValue objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:@"alert"];
}
if (message)
{
if (![message isEqualToString:@""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"notification"
message: message
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
....
当然,您可能希望创建一个处理通知的特殊方法,并从两个方案(使用NSDictionary
*参数)调用,以便您的代码更具可读性。 有时,APNS通知在app运行时也很有用 - 可能会使用空通知(没有有效负载)来触发与服务器的数据同步,以避免轮询。
当应用程序以下面的代码启动时,您可以获取到达的通知(例如:在应用程序中:didFinishLaunchingWithOptions):
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
以下是一个更全面的解释: 如何在用户点击徽章时管理通知
你可以这样处理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Checking if app was launched from the notification
if (launchOptions != nil) {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil){
// Read dictionary and do something since the app
// was launched from the notification.
}
}
这是字典对象包含的示例
NSString *message = @"";
NSString *badge = @"";
NSString *sound = @"";
if([dictionary objectForKey:@"alert"]) {
message = [dictionary objectForKey:@"alert"];
}
if([dictionary objectForKey:@"badge"]) {
badge = [dictionary objectForKey:@"badge"];
}
if([dictionary objectForKey:@"sound"]) {
sound = [dictionary objectForKey:@"sound"];
}
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.