Q1。我必须在我的应用程序开始时这样做吗?或者我可以在我的应用程序的任何时候触发允许/不允许的提示吗?
Q2。有没有办法找出用户是否点击了是/否?(打回来?)
Q3。如果用户已经点击了否(在之前的会话中),我的提示是否真的会触发?还是我需要告诉用户去他们的手机设置并在那里启用它?
原因是我有一个应用程序,其中有一个名为“通知”的部分,他们可以启用/禁用接收某些事情的通知,所以我只想提示他们启用等,当他们在这个部分而不是在应用程序的启动。
Q1。我必须在我的应用程序开始时这样做吗?或者我可以在我的应用程序的任何时候触发允许/不允许的提示吗?
Q2。有没有办法找出用户是否点击了是/否?(打回来?)
Q3。如果用户已经点击了否(在之前的会话中),我的提示是否真的会触发?还是我需要告诉用户去他们的手机设置并在那里启用它?
原因是我有一个应用程序,其中有一个名为“通知”的部分,他们可以启用/禁用接收某些事情的通知,所以我只想提示他们启用等,当他们在这个部分而不是在应用程序的启动。
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: 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
}
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: 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.
See Apple Push Notification docs for details on implementation.
application: didFinishLaunchingWithOptions:
以确保设备令牌成功存储在开发人员的服务器上。application:
didRegisterForRemoteNotificationsWithDeviceToken
则将触发委托,否则不会。[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
再次触发。您可以使用以下教程进行 swift(访问链接)并使用以下简单代码用于 Apple 推送通知的 Objective-c。
- (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);
}
回答问题: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);
}];