ios本地推送互動

來源:互聯網
上載者:User

標籤:

?    小編只是一個小白,每個app都需要推送功能,個人覺得是基礎中的基礎,所以自學了下推送,並且寫了點小demo,也是第一次發原創部落格,還請各路大神批評指導。

    資訊推送可以讓App尚未啟動並執行狀態下可以接受一些資訊,通常的推送有蘋果內建的APNS推送,這個方面的知識還請自行查詢學習,本文只是針對本地推送互動方面,後續會單獨寫APNS推送的博文,還請持續關注!

    推送資訊文字主要出現四個位置:1.螢幕鎖定畫面;2.螢幕上方下拉的“通知區域”;3.解鎖後螢幕最上方的橫幅的位置;4解鎖後的提示位置;解鎖模式下推送資訊的位置需要在自己去調整 在系統--->通知--->要調整的App下面調整

  

    推送資訊的處理有互動按鈕(本文使用四個),可以把這四個按鈕放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,封裝在UIMutableUserNOtificationCategory類中,一個App可以有很多組Category,以供推送資訊時選 擇,最後將category封裝到NSSet中,註冊到UIUserNotificationSettings中。

代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    UIMutableUserNotificationAction *action1=[UIMutableUserNotificationAction new];    action1.identifier=@"ACTION_1";    action1.title=@"title";    action1.activationMode=UIUserNotificationActivationModeForeground;    action1.authenticationRequired=YES;    action1.destructive=NO;        UIMutableUserNotificationAction *action2=[UIMutableUserNotificationAction new];    action2.identifier=@"ACTION_2";    action2.title=@"title2";    action2.authenticationRequired=NO;    action2.activationMode=UIUserNotificationActivationModeBackground;    action2.destructive=YES;        UIMutableUserNotificationAction *action3=[UIMutableUserNotificationAction new];    action3.identifier=@"ACTION_3";    action3.title=@"title3";    action3.activationMode=UIUserNotificationActivationModeBackground;    action3.authenticationRequired=NO;    action3.destructive=NO;;        UIMutableUserNotificationAction *action4=[UIMutableUserNotificationAction new];    action4.identifier=@"ACTION_4";    action4.title=@"titile4";    action4.activationMode=UIUserNotificationActivationModeBackground;    action4.authenticationRequired=NO;    action4.destructive=NO;        UIMutableUserNotificationCategory *category=[UIMutableUserNotificationCategory new];    category.identifier=@"CATEGORY";    [category setActions:@[action1,action2,action3,action4] forContext:UIUserNotificationActionContextDefault];    NSSet *categories=[NSSet  setWithObject:category];    UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories];    [application registerUserNotificationSettings:setting];    return YES;}

 

這其中:identifier是用來識別使用者點擊了哪個按鈕的;title是按鈕的文字;activationMode是指這個按鈕按下去是不是要把app調到前台運行,設定為前台運行就需要賦值為YES;authenticationRequired是只當裝置屬於鎖定狀態時候,是否要求使用者先解鎖;destructive是標識特別重要的按鈕,操作他可能帶來一定的影響,例如刪除資料。

    互動操作需要在appdelega.m的一個方法中寫互動操作方法代碼如下:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{    if ([notification.category isEqualToString:@"CATEGORY"]) {        if ([identifier isEqualToString:@"ACTION_1"]) {            NSLog(@"單擊了Category1,Action1");        }        if ([identifier isEqualToString:@"ACTION_2"]) {            [UIApplication sharedApplication].applicationIconBadgeNumber=0;            NSLog(@"單擊了Category2,Action2");        }        if ([identifier isEqualToString:@"ACTION_3"]) {            NSLog(@"單擊了Category3,Action3");        }        if ([identifier isEqualToString:@"AACTION_4"]) {            NSLog(@"單擊了Category4,Action4");        }    }    completionHandler();//必寫}

 

最後在viewDidLoad寫(圖方便)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    UILocalNotification *note=[UILocalNotification new];    note.alertBody=@"自訂推播通知";    note.category=@"CATEGORY";    note.applicationIconBadgeNumber=1;    note.soundName=UILocalNotificationDefaultSoundName;    note.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];    [[UIApplication sharedApplication] scheduleLocalNotification:note];}

 

謝謝!初次寫,不足之處請諒解!!!

ios本地推送互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.