处理苹果推送通知服务

3
我在我的项目中使用了苹果推送通知服务。
请按照以下的两种方式打开应用并处理推送通知。在第二种情况下,我不知道如何处理它。你知道怎么做吗?
推送通知已经到达我的设备,
情景1: 1. 我点击了推送通知。 2. 在AppDelegate.m文件中,函数- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo捕获到了该函数。
情景2: 1. 我正常地打开了设备(通过点击应用程序)。 2. 我该如何处理推送通知?

1
看起来第二种情况不是问题。通常,当用户通过触摸应用程序图标启动应用程序时,这意味着他不希望从推送通知中触发操作。 顺便说一下,如果您想轻松实现推送通知,可以查看PushApps的以下SDK http://www.pushapps.mobi/ - Raz
4个回答

3

其他答案展示了如何在用户点击通知时获取通知数据。

两种方法的区别在于,一种在应用程序已经运行(无论是在前台还是后台)时调用,而另一种在应用程序根本没有运行时调用。

对于您的第二种情况,当用户不点击通知时,如果您使用启动图标打开应用程序,则不会将通知数据传递给应用程序。


你能给我建议一个处理这种情况(应用程序通过启动图标打开)的新通知的模式吗? - ankakusu

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通知也很有用-空通知(没有有效负载)可以用于触发数据与服务器的同步,以避免轮询等情况。

0

当应用程序启动时,您可以使用以下代码(例如:在application:didFinishLaunchingWithOptions中)获取到已到达的通知:

NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

这里有一个更详细的解释:如何在用户点击徽章时管理通知

0

你可以像这样处理它

- (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"]; 
}

希望能对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接