iOS注册应用程序中的推送通知 [英] iOS Registering for Push Notifications within Application

查看:126
本文介绍了iOS注册应用程序中的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Q1。在我的应用程序开始时我是否必须这样做?或者我可以在我的应用程序的任何位置触发允许/不允许的提示吗?

Q1. Do I have to do this at the start of my App? Or can I trigger the prompt to allow/not-allow at any point in my app?

Q2。有没有办法找出用户是否点击了是/否? (回调?)

Q2. Is there a way to find out if the user clicked yes/no ? (callback?)

Q3。如果用户已经单击否,(在之前的会话中),我的提示是否会实际触发?或者我是否需要告诉用户转到他们的手机设置并在那里启用?

Q3. If the user has already clicked no, (in a previous session), will my prompt actually fire? Or do I need to tell the user to go to their phone settings and enable it there?

原因是我有一个应用程序,里面有一个名为通知的部分他们可以启用/禁用接收某些事情的通知,所以我只想提示他们启用等等,当他们在本节不在应用程序的开头。

Reason being is I have an app which has a section inside it called "Notifications" whereby they can enable/disable to receive notifications on certain things, so I only want to prompt them to enable etc. when they are in this section not at the start of the App.

推荐答案

A1:不,它不必在应用程序的开头。您可以从代码中的任何位置调用registerForRemoteNotificationTypes。

A1: No, it doesn't have to be at the start of the app. You can invoke registerForRemoteNotificationTypes from anywhere in the code.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

您将需要处理以下委托方法(在委托中),这些方法在成功/失败注册时被调用推送通知。

You will need to handle following delegates methods(in delegate) which gets called on successful/fail registering for push notification.

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes]; // this will send token to your server's database
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

A2:是的,你可以。有两种可能的情况。如果您的应用程序未运行,您将在didFinishLaunchingWithOptions中处理推送通知。在这种情况下,如果用户在消息提醒中选择打开或单击横幅(取决于用户的设置),您的应用程序将自动启动,您可以处理推送通知中传递的用户参数。

A2: Yes you can. There are two possible scenarios. If your application is not running, you will handle push notification in didFinishLaunchingWithOptions. In this case if user has selected "open" in the message alert or clicked on Banners(depends on user's settings), your application will automatically start and you can handle user parameters passed in push notification.

 /* Push notification received when app is not running */
 NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"];

    if (params) {
        // Use params and do your stuffs
    }

如果您的应用程序已在运行,推送通知将被传递到应用程序:didReceiveRemoteNotification:委托方法,您可以在其中简单地向UIAlertView显示消息在推送通知和处理alertView委托确定/取消按钮以标准方式单击。

If your application is already running, push notification will be delivered to application:didReceiveRemoteNotification: delegate method where you can simply present UIAlertView with the message in push notification and handle alertView delegates OK/Cancel button click in standard way.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSDictionary *apsInfo = [userInfo objectForKey:@"apsinfo"]; // This appsInfo set by your server while sending push

    NSString *alert = [apsInfo objectForKey:@"alert"];

    UIApplicationState state = [application applicationState];

    if (state == UIApplicationStateActive) {
        application.applicationIconBadgeNumber = 0;

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Push Notification"
                                                            message:alert
                                                           delegate:self
                                                  cancelButtonTitle:@"NO"
                                                  otherButtonTitles:@"YES"];
        [alertview show];
        [alertview release];

    } else {
        [self setTabs:contentsInfo];
     }
}


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
     // User pressed YES, do your stuffs
     }
}

A3:如果用户拒绝接受来自您的应用的推送通知,那么它的didFailToRegisterForRemoteNotificationsWithError就会因此而导致您无法获得用户的devToken,该服务器需要在该服务器上向该用户发送推送通知。如果用户最初接受但稍后如果他更改了设置以禁用推送通知,那么Apple服务器将不会将推送通知发送给该用户。在这种情况下,用户的UDID将出现在反馈服务中,理想情况下,您的服务器应从数据库中删除该用户的UDID,并停止向该用户发送推送通知。如果您继续发送无效推送通知,Apple服务器可能会静默删除您的连接,您将无法发送任何推送通知。

A3: If user has refused to accept push notification from your app then its didFailToRegisterForRemoteNotificationsWithError and you hence you will not get user's devToken which is required to be on your server to send push notification to that user. If user initially accept but later on if he changed settings to disable your push notification then Apple server will not send your push notification to that user. In this case that user's UDID will appear in Feedback service and ideally your server should remove that user's UDID from the database and stop sending push notification to that users going forward. If you keep sending invalid push notification, Apple server might drop your connection silently and you will not able to send any push notifications.

参见 Apple推送通知文档,了解有关实施的详细信息。

See Apple Push Notification docs for details on implementation.

这篇关于iOS注册应用程序中的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆