IOS 本地通知 UILocalNotification


【本文章第四部分中的代碼邏輯來自網上的借鑒,並非我自己原創】

大概一個月前,我開始跟着做IOS項目了。學習C++,了解Objective-C,等等。這兩天做了游戲的本地通知,當然是從網上查了很多資料。

但資料有很多的偏差,不過最終還是解決了問題,很幸運。所以總結了一下下。

用到的重點就是Objective-C 的UILocalNotification對象。其實加入通知的代碼很簡單,但重要的是你要理順Notification的邏輯。

首先我要聲明的是我的開發環境:

首先在windows下面用Visual studio開發,調試,編譯通過了以后。再在Mac下面用Xcode編譯,導到Ipad4上面運行的。所以我的混編文件是Objective-C和C++的混合。

 

 1 UILocalNotification *notification=[[UILocalNotification alloc] init];
 2         if (notification!=nil) {
 3             NSDate *now = [NSDate date];
 4             //從現在開始,10秒以后通知
 5             notification.fireDate=[now dateByAddingTimeInterval:10];
 6             //使用本地時區
 7             notification.timeZone=[NSTimeZone defaultTimeZone];
 8             notification.alertBody=@"頂部提示內容,通知時間到啦";
 9             //通知提示音 使用默認的
10             notification.soundName= UILocalNotificationDefaultSoundName;
11             notification.alertAction=NSLocalizedString(@"你鎖屏啦,通知時間到啦", nil);
12             //這個通知到時間時,你的應用程序右上角顯示的數字。
13             notification.applicationIconBadgeNumber = 1;
14             //add key  給這個通知增加key 便於半路取消。nfkey這個key是我自己隨便起的。
15             // 假如你的通知不會在還沒到時間的時候手動取消 那下面的兩行代碼你可以不用寫了。
16             NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],@"nfkey",nil];
17             [notification setUserInfo:dict];
18             //啟動這個通知
19             [[UIApplication sharedApplication]   scheduleLocalNotification:notification];
20             //這句真的特別特別重要。如果不加這一句,通知到時間了,發現頂部通知欄提示的地方有了,然后你通過通知欄進去,然后你發現通知欄里邊還有這個提示
21             //除非你手動清除,這當然不是我們希望的。加上這一句就好了。網上很多代碼都沒有,就比較郁悶了。
22             [notification release];
23         }

首先,這是一段Objective-C的代碼,所以這段代碼肯定也必須處在一個混編文件中,也就是后綴為.mm的文件里邊。里面的內容我已經注釋的很清楚,太詳細的大家去查文檔就可以了。
其中要特別注意的是第13行和22。

這段代碼是從現在開始,多長時間后觸發,並且只觸發一次,這是我的需求,如果你需要固定時間觸發,循環觸發,也可以實現,google一下就可以了。

就這么簡單,你調用了這段代碼后,將app切到后台或者直接Kill掉,10秒后,就會看到頂部通知欄的通知,並且圖標右上角顯示紅色的1。然后你通過通知欄點進去,會啟動app,然后你再看,通知欄沒有這個通知了,但是,app圖標的右上角的那個1還是在的。

所以,這段代碼很簡單,但是你重要的還是邏輯。我介紹一下我的邏輯,當然我只是一個菜鳥,你取其中自認為有價值的地方即可。

1 創建一個基類CCNotificationUtil,便於在C++代碼中調用。這個我是放在cocos2d命名空間下的。這樣在C++代碼里面,需要增加通知的地方,比如你的建築開始升級了,你收到服務器傳來的升級時間,你只需要調用

cocos2d::CCNotificationUtil::shareNotificationUtil()->addNotification(str_Alert,notificatioTag,timesec);*/

其中的三個參數,分別是通知欄顯示的內容,通知的key,從現在開始多長時間以后觸發。再重復一下,只會觸發一次。這個類里面還有一個removeNotification方法,這個就是你半路取消通知時需要調用的方法,參數是創建時傳入的tag。比如你點了升級建築,開始生了,一個小時后完成。過了半個小時,你覺得時間太長了,用錢加速吧,這個時候,就需要調用這個方法了,因為你之前已經加了通知,告訴IOS一個小時后提示,這個時候你就的從IOS的通知里面把它給去掉了,所以這個方法就是滿足這個需求的。

 CCNotificationUtil.h代碼:

 1 #ifndef _CC_NotificationUtil_H__
 2 #define _CC_NotificationUtil_H__
 3 
 4 #include "ccTypes.h"
 5 #include "ccTypeInfo.h"
 6 
 7 NS_CC_BEGIN
 8 class CC_DLL CCNotificationUtil : public TypeInfo
 9 {
10 protected:
11     CCNotificationUtil();
12     static CCNotificationUtil* s_sharedNotificationUtil;
13     int m_pIconBadgeNumber;
14 public:
15     virtual long getClassTypeInfo() {
16         static const long id = cocos2d::getHashCodeByString(typeid(cocos2d::CCNotificationUtil).name());
17         return id;
18     }
19     static CCNotificationUtil * shareNotificationUtil();
20     virtual ~CCNotificationUtil();
21     virtual bool addNotification(std::string alerttext,int notificationtag,int timeinterval);
22     virtual bool init();
23     virtual bool removeNotification(int notificationtag);
24 };
25 NS_CC_END
26 #endif // !_CC_NotificationUtil_H_

CCNotificationUtil.app代碼

 1 #include "CCNotificationUtil.h"
 2 
 3 NS_CC_BEGIN
 4 
 5 CCNotificationUtil* CCNotificationUtil::s_sharedNotificationUtil = NULL;
 6 
 7 CCNotificationUtil::CCNotificationUtil()
 8     :m_pIconBadgeNumber(0)
 9 {
10 
11 }
12 CCNotificationUtil::~CCNotificationUtil()
13 {
14 
15 }
16 bool CCNotificationUtil::init()
17 {
18     return true;
19 }
20 bool CCNotificationUtil::addNotification(std::string alerttext,int notificationtag,int timeinterval)
21 {
22     return true;
23 }
24 bool CCNotificationUtil::removeNotification(int notificationtag)
25 {
26     return ( true );
27 }
28 
29 NS_CC_END

2 創建和IOS平台相關的子類。這個才是做實際操作的類,因為會使用Objective-C,所以這是個混編文件。在你的項目中和平台相關的文件夾platform/ios/下面創建CCNotificationUtilIOS類。其中傳入的參數會有C++和Objective-C的相互轉換。
CCNotificationUtilIOS.h

#ifndef _CC_NotificationUtilIOS_H__
#define _CC_NotificationUtilIOS_H__

#include "ccTypes.h"
#include "ccTypeInfo.h"
#include "CCNotificationUtil.h"
NS_CC_BEGIN
class CC_DLL CCNotificationUtilIOS: public CCNotificationUtil
{
public:
    // override function
    virtual bool addNotification(std::string alerttext,int notificationtag,int timeinterval);
    virtual bool init();
    virtual bool removeNotification(int notificationtag);
};
NS_CC_END
#endif //_CC_NotificationUtilIOS_H__

CCNotificationUtilIOS.mm

#import "cocos2d.h"


#include "CCNotificationUtil.h"
#include "CCNotificationUtilIOS.h"
NS_CC_BEGIN

CCNotificationUtil* CCNotificationUtil::shareNotificationUtil()
{
    if(s_sharedNotificationUtil== NULL)
    {
        s_sharedNotificationUtil = new CCNotificationUtilIOS();
        s_sharedNotificationUtil->init();
    }
    return s_sharedNotificationUtil;
}
bool CCNotificationUtilIOS::init()
{
    if(CCNotificationUtil::init()==true)
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        return true;
    }
    return false;
}
bool CCNotificationUtilIOS::addNotification(std::string alerttext,int notificationtag,int mytimeinterval)
{
    if(CCNotificationUtil::addNotification(alerttext,notificationtag,mytimeinterval)==true)
    {
        UILocalNotification *notification=[[UILocalNotification alloc] init];
        if (notification!=nil) {
            NSDate *now = [NSDate date];
            notification.fireDate=[now dateByAddingTimeInterval:mytimeinterval];
            notification.timeZone=[NSTimeZone defaultTimeZone];
            notification.alertBody=[NSString stringWithUTF8String:alerttext.c_str()];
            notification.soundName= UILocalNotificationDefaultSoundName;
            notification.alertAction=NSLocalizedString([NSString stringWithUTF8String:alerttext.c_str()], nil);
            notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
            //add key
            NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],@"nfkey",nil];
            [notification setUserInfo:dict];
            [[UIApplication sharedApplication]   scheduleLocalNotification:notification];
            [notification release];
            return true;
        }
    }
    return false;
}
bool CCNotificationUtilIOS::removeNotification(int notificationtag)
{
    NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications];
    NSUInteger acount=[narry count];
    if (acount<1) {
        return false;
    }
    for (int i=0; i<acount; i++) {
        UILocalNotification *myUILocalNotification = [narry objectAtIndex:i];
        NSDictionary *userInfo = myUILocalNotification.userInfo;
        NSNumber *obj = [userInfo objectForKey:@"nfkey"];
        int mytag=[obj intValue];
        if (mytag==notificationtag) {
            [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
            return true;
        }
    }
    return false;
}

NS_CC_END

特別注意的是設置右上角圖標個數的代碼,如下。就是說當我這個通知到時間時,顯示的個數,應該是在我之前加入到通知schedule里面的數量,當然這個不是絕對的,比如你第一個通知是一個小時后提示,緊接這觸發了第二個通知,而第二個是10分鍾,顯然10分鍾通知提示時,右上角會顯示2,但其實應該是1。這個問題需要考慮,后面第4步我會說我的解決方法。

 notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;

 

 

3 因為我開的需求之前介紹過在Win32上調試的,所以需要一個win32平台實現的子類,保證編譯通過,但實際上這個類什么也不做。如果你不需要在win32上調試,這步可以省略了。在ios平級目錄win32下創建CCNotificationUtilWin32子類。

CCNotificationUtilWin32.h

#ifndef _CC_NotificationUtilWin32_H__
#define _CC_NotificationUtilWin32_H__
#include "platform/CCNotificationUtil.h"
#include "platform/CCPlatformMacros.h"
#include "ccTypes.h"
#include "ccTypeInfo.h"

NS_CC_BEGIN
    
/**
 * @addtogroup platform
 * @{
 */

//! @brief  Helper class to handle file operations
class CCNotificationUtilWin32 : public CCNotificationUtil
{
    friend class CCNotificationUtil;
    CCNotificationUtilWin32();
public:
    // override function
    virtual bool addNotification();
    virtual bool init();
    virtual bool removeNotification(int notificationtag);
};
NS_CC_END

    // end of platform group
    /// @}


#endif // !_CC_NotificationUtilWin32_H__

CCNotificationUtilWin32.app

#include "CCNotificationUtilWin32.h"
#include "platform/CCCommon.h"
#include <Shlobj.h>
NS_CC_BEGIN
CCNotificationUtil* CCNotificationUtil::shareNotificationUtil()
{
    if(s_sharedNotificationUtil== NULL)
    {
        s_sharedNotificationUtil = new CCNotificationUtilWin32();
        s_sharedNotificationUtil->init();
    }
    return s_sharedNotificationUtil;
}

CCNotificationUtilWin32::CCNotificationUtilWin32()
{
}

bool CCNotificationUtilWin32::init()
{
    return CCNotificationUtil::init();
}
bool CCNotificationUtilWin32::addNotification()
{
    return true;
}
bool CCNotificationUtilWin32::removeNotification(int notificationtag)
{
    return true;
}
NS_CC_END

4 一個右上角提示的問題。你觸發了5個通知,后台運行的情況下,3個已經到了,所以你的app右上角此時顯示3。那么你通過通知欄或者直接點app進去以后,app右上角應該變為0。按照之前在addNotification里面的設置,等到第4個通知到了以后,app右上角會顯示4,第五個到了以后,會顯示5,很明顯這是不對的。所以我們需要在通過通知欄進去,或者直接點app進去以后,重新去設置第四個第五個通知到了以后,app右上角的數字。
這個當然也需要在混編文件中。我是在實現UIApplicationDelegate的混編文件中重寫了兩個方法didReceiveLocalNotification和applicationDidBecomeActive。

didReceiveLocalNotification是app在前台運行,通知時間到了,調用的方法。如果程序在后台運行,時間到了以后是不會走這個方法的。

applicationDidBecomeActive是app在后台運行,通知時間到了,你從通知欄進入,或者直接點app圖標進入時,會走的方法。

里面的邏輯,就是找到所有加入schedule的通知,重新設置通知時間到時,app右上角的數字。

 

 1     - (void)applicationDidBecomeActive:(UIApplication *)application {
 2     /*
 3      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 4      */
 5     //reset applicationIconBadgeNumber;
 6     application.applicationIconBadgeNumber=0;
 7     int count =[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
 8     if(count>0)
 9     {
10         NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0];
11         for (int i=0; i<count; i++) {
12             UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
13             notif.applicationIconBadgeNumber=i+1;
14             [newarry addObject:notif];
15         }
16         [[UIApplication sharedApplication] cancelAllLocalNotifications];
17         if (newarry.count>0) {
18             for (int i=0; i<newarry.count; i++) {
19                 UILocalNotification *notif = [newarry objectAtIndex:i];
20                 [[UIApplication sharedApplication] scheduleLocalNotification:notif];
21             }
22         }
23     }
24     
25     cocos2d::CCDirector::sharedDirector()->resume();
26 }
27 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
28     if (notification)
29     {
30         application.applicationIconBadgeNumber=0;
31         int count =[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
32         if(count>0)
33         {
34             NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0];
35             for (int i=0; i<count; i++) {
36                 UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
37                 notif.applicationIconBadgeNumber=i+1;
38                 [newarry addObject:notif];
39             }
40             [[UIApplication sharedApplication] cancelAllLocalNotifications];
41             if (newarry.count>0) {
42                 for (int i=0; i<newarry.count; i++) {
43                     UILocalNotification *notif = [newarry objectAtIndex:i];
44                     [[UIApplication sharedApplication] scheduleLocalNotification:notif];
45                 }
46             }
47         }
48     }
49 }

5 我們在實現UIApplicationDelegate的混編文件中還要實現一個方法didFinishLaunchingWithOptions。這是程序首次啟動時,會走的地方。在這里,我們需要設置app右上角的數字為0,並且要清空所有的本地通知。為什么?比如,還是你升那個1小時的建築,你點了以后,然后kill掉app了,但是那個通知已經放到本地了。然后你重新啟動了程序,你收到服務器來的數據,顯示建築還有30分鍾才到,你又給本地加了一個Notification。這樣的話,你在30分鍾和一個小時后,都會收到通知,這個肯定不會的吧。所以,加入這兩行代碼就可以了。

當然也有一種解決方法時,你加入的時候根據tag判斷一下,如果有了,刪除,重加。只是這個個人感覺比較麻煩,也不通用。

   // about notification
    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];


好了,這就大功告成了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM