[iuud8]coocs2dx之ios本機推送

iOS下推送可以分爲本地推送和服務器推送兩種。本地的推送對象是UILocalNotification.由iOS下NotificationManager統一管理,只需要將封裝好的本地Notification對象加入到系統Notification管理機制隊列中,系統會在指定的時間激發將本地Notification,應用只需設計好處理Notification的方法就完成了整個Notification流程了.

對於cocos2dx來說,因爲需要使用oc來實現,因此,在AppController.mm文件中實現該功能。具體實現:

    //推送

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"HH:mm:ss"];

    NSDate *now = [formatter dateFromString:@"9:30:00"]; //觸發通知的時間


    UILocalNotification *notification1=[[[UILocalNotification alloc] init] autorelease];

    if(notification1!=nil){

        notification1.fireDate = now; //觸發通知的時間

        notification1.timeZone=[NSTimeZone defaultTimeZone]; //設置時區

        notification1.repeatInterval=kCFCalendarUnitDay; //設置重複間隔

        notification1.soundName = UILocalNotificationDefaultSoundName; //設置推送聲音

        notification1.alertBody=@"親愛的,準備好了嗎?該去打敗女巫咯!"; //設置推送內容

        notification1.applicationIconBadgeNumber=application.applicationIconBadgeNumber+1; //顯示在icon右上角的紅色數字,每次+1


//設置userinfo方便之後需要撤銷的時候使用

        NSString *bundleIdentifier =[[NSBundle mainBundle] bundleIdentifier]; //觸發通知的時間

        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:bundleIdentifier forKey:@"Identifier"]; //觸發通知的時間

        notification1.userInfo=infoDic; //觸發通知的時間

 

        [application scheduleLocalNotification:notification1];

    }


因爲設定每次推送讓icon'右上角的數字+1,因此,我們需要在激活遊戲的時候設定icon右上角的數字爲0,在applicationDidBecomeActive函數中實現:

- (void)applicationDidBecomeActive:(UIApplication *)application {

    application.applicationIconBadgeNumber = 0;

}

發佈了24 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章