Q1. 我需要在应用程序启动时执行这个操作吗?还是说我可以在应用程序的任何时候触发允许/不允许提示?
Q2. 是否有方法可以找出用户是否点击了是/否(回调)?
Q3. 如果用户以前已经点击了“否”(在之前的会话中),那么我的提示是否会实际触发?或者我需要告诉用户去他们的手机设置里启用它?
原因是我有一个名为“通知”的部分的应用程序,用户可以在其中启用/禁用某些事物的通知,所以当他们在此部分而不是在应用程序的开始时,我只想提示他们启用等等。
Q1. 我需要在应用程序启动时执行这个操作吗?还是说我可以在应用程序的任何时候触发允许/不允许提示?
Q2. 是否有方法可以找出用户是否点击了是/否(回调)?
Q3. 如果用户以前已经点击了“否”(在之前的会话中),那么我的提示是否会实际触发?或者我需要告诉用户去他们的手机设置里启用它?
原因是我有一个名为“通知”的部分的应用程序,用户可以在其中启用/禁用某些事物的通知,所以当他们在此部分而不是在应用程序的开始时,我只想提示他们启用等等。
A1:不,它不必在应用程序的开头。您可以从代码的任何位置调用registerForRemoteNotificationTypes。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// 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中处理推送通知。在这种情况下,如果用户在消息警报中选择了“打开”或点击了横幅(取决于用户的设置),你的应用程序将自动启动,你可以处理传递的用户参数。
/* Push notification received when app is not running */
NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"];
if (params) {
// Use params and do your stuffs
}
application:didReceiveRemoteNotification:
代理方法中,您可以在其中简单地展示带有推送通知中消息的UIAlertView,并以标准方式处理alertView代理OK/Cancel按钮点击。- (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
}
}
[application registerForRemoteNotifications]
,因为这将为您获取令牌(您可以在不需要用户权限的情况下执行此操作;只有在向用户提供警报/徽章、声音时才需要权限)。在iOS 10中,有一个名为“UserNotifications”的新框架,它将允许您找出用户是否允许访问。请参见这里了解更多关于iOS 10的信息(1/2)。 - mfaani[application isRegisteredForRemoteNotifications]
来查看是否已注册。(2/2) - mfaani - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Did Fail to Register for Remote Notifications");
NSLog(@"%@, %@", error, error.localizedDescription);
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"%@",userInfo);
}
application: didFinishLaunchingWithOptions:
中放置它以确保设备令牌成功存储到开发人员的服务器上。application: didRegisterForRemoteNotificationsWithDeviceToken
,否则不会。[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
iOS 10中第2个问题的答案:
iOS 10中实现了完成处理程序。因此,您将立即在完成处理程序块内得到通知。
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.f){
UNUserNotificationCenter* notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"grant Access:%d",granted);
}];
UserNotifications
框架,则可以通过回调函数找出用户是否点击了是/否。请参见此处。 - mfaani