iOS判断用户是否打开APP通知开关 iOS判断用户是否打开APP通知开关

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

iOS判断用户是否打开APP通知开关 iOS判断用户是否打开APP通知开关

KennyHito   2021-03-24 我要评论
想了解iOS判断用户是否打开APP通知开关的相关内容吗,KennyHito在本文为您仔细讲解iOS判断用户是否打开APP通知开关的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:iOS,APP,通知开关,下面大家一起来学习吧。

一.前言

  在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情。iOS中通知机制又叫消息机制,其包括两类:一类是本地通知;另一类是推送通知,也叫远程通知。两种通知在iOS中的表现一致,可以通过横幅或者弹出提醒两种形式告诉用户,并且点击通知可以会打开应用程序,但是实现原理却完全不同。

二.代码如下

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   //开启通知

 if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {

  [self addLocalNotification];

 }else{

  [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

 }

 

 return YES;

}

 

#pragma mark 添加本地通知

-(void)addLocalNotification{

  //定义本地通知对象

 UILocalNotification *notification=[[UILocalNotification alloc]init];

  //调用通知

 [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

 

#pragma mark 移除本地通知,在不需要此通知时记得移除

-(void)removeNotification{

 [[UIApplication sharedApplication] cancelAllLocalNotifications];

}

@end

 三.效果图

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们