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:是的,你可以。有两种可能的情况。如果您的应用程序未运行,则您将处理finishlaunchingwithoptions中的推送通知。在这种情况下,如果用户在消息警报中选择了"打开"或单击横幅(取决于用户的设置),则您的应用程序将自动启动,您可以处理推送通知中传递的用户参数。
/* 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
}
}
a3:如果用户拒绝接受您的应用程序的推送通知,则其didFailToreGisterForremoteNotificationationswitherror,因此您将不会获得用户的Devtoken,而用户的Devtoken是在服务器上需要将推送通知发送给该用户的devtoken。如果用户最初接受但稍后他是否更改了设置以禁用您的推送通知,则Apple Server不会将您的推送通知发送给该用户。在这种情况下,用户的UDID将出现在反馈服务中,理想情况下,您的服务器应从数据库中删除该用户的UDID,并停止向前进的用户发送推送通知。如果您继续发送无效的推送通知,Apple Server可能会静静地删除连接,并且您将无法发送任何推送通知。
有关实施的详细信息,请参见Apple推送通知文档。
- Q1不必在应用程序发布时将登记处放置,但有些则是将其放在
application: didFinishLaunchingWithOptions:
中以某种方式确保设备令牌成功存储在开发人员的服务器上。 - q2这里是什么"是/否"?如果您是说"是/否",则接收推送通知",然后如果用户单击"是",则委托
application: didRegisterForRemoteNotificationsWithDeviceToken
将被触发,否则不会。 - Q3如果用户单击"否"以拒绝接收推送通知,则以后您可以提醒他在设置中打开它,或者有一个功能,例如UISWitch的东西,使用户能够触发
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
再次。
您可以使用swift教程(访问link)的遵循教程,并使用以下简单代码作为Apple Push Notification。
- (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);
}];