零基礎教你Unity接入IOS原生本地推送
一,新建Unity項目
二,梳理程序流程
三,處理代碼邏輯
四,測試場景搭建
五,處理自動配置
六,雙端打包測試
七,查看測試結果
從新建項目開始的保姆級教程,教你Unity接入IOS原生本地推送。
一,新建Unity項目
打開Unity Hub,點擊 ”新建“ , 輸入項目名稱,選擇存儲位置,點擊創建即可。
創建后Unity會自動打開,我們先創建幾個文件夾
IOSLocalNotification: 總目錄,存儲本次實例使用的所有相關文件
Editor: 二級文件夾,存儲打包Xcode的自動配置腳本
Plugins: 存儲對接IOS的橋接文件
Scenes: 存儲場景,直接把創建項目的帶進來就行了
Scripts: 存儲項目Unity的.cs腳本
修改場景名稱為 ”LocalNotification“, 然后"場景從外部修改了" 我們點擊 ”Reload“ 重新加載下就可以了。
至此創建Unity項目和準備工作完成。
二,梳理程序流程
點擊按鈕觸發本地推送
代碼調用IOS橋接文件
IOS橋接文件調用IOS原生
IOS原生處理本地推送
點擊按鈕觸發本地推送
代碼調用IOS橋接文件
IOS橋接文件調用IOS原生
IOS原生處理本地推送
三,處理代碼邏輯
IOS端代碼邏輯:
IOS端代碼的編寫,顯然超出了本文的范疇(我也不會)。其實我們只要知道Unity代碼如何能調用IOS的代碼調用就可以了
比如,IOS代碼:
// 移除所有通知 - (void)removeAllNotification { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllPendingNotificationRequests]; }else { [[UIApplication sharedApplication] cancelAllLocalNotifications]; } }
1
2
3
4
5
6
7
8
9
在Unity中的調用
#if UNITY_IOS using System.Runtime.InteropServices; #endif public class LocalNotification_IOS : MonoBehaviour { #if UNITY_IOS // 移除所有推送 -- 調用到IOS [DllImport("__Internal")] private static extern void _removeAllNotification(); #endif /// /// 移除所有推送 /// public void RemoveAllNotification() { _removeAllNotification(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
這樣寫就實現了,從Unity調用到IOS了。
流程: Unity按鈕點擊觸發RemoveAllNotification()方法 --> 方法體調用 _removeAllNotification() 通過[DllImport("__Internal")]關聯到IOS --> 調用到IOS中的 (void)removeAllNotification。
由于篇幅問題,IOS端完整代碼我就不貼出來了,需要的童鞋點擊本段末鏈接下載即可。
Unity代碼邏輯:
創建LocalNotification_IOS.cs 放到Scripts文件夾下,此腳本處理對接IOS端代碼邏輯:
using System.Collections; using System.Collections.Generic; using System; using UnityEngine; #if UNITY_IOS using System.Runtime.InteropServices; #endif /// /// 本地推送IOS -- 調用橋接文件 /// public class LocalNotification_IOS : MonoBehaviour { // 單例 public static LocalNotification_IOS Instance; #region 當首次授權成功后需要重新添加首次的推送消息,以下變量用于首次推送時儲存推送內容 private string titleStr; private string subtitleStr; private string contentStr; private int time; private string identifier; #endregion private void Awake() { Instance = this; // 切換場景不銷毀 DontDestroyOnLoad(this.gameObject); // 移除所有推送 #if !UNITY_EDITOR RemoveAllNotification(); #endif } #if UNITY_IOS // 對接IOS端代碼邏輯 [DllImport("__Internal")] private static extern void _addLocalNotice(string titleStr,string subtitleStr,string contentStr,int time,string identifier); [DllImport("__Internal")] private static extern void _registerAPN(); [DllImport("__Internal")] private static extern void _removeOneNotificationWithID(string noticeId); [DllImport("__Internal")] private static extern void _removeAllNotification(); [DllImport("__Internal")] private static extern bool _haveNoticeNotifocation(); [DllImport("__Internal")] private static extern void _checkPermission(); [DllImport("__Internal")] private static extern void _showSettingAlert(string title, string content, string leftTxt, string rightTxt); /// /// 注冊APN推送通道 /// private void RegisterAPN() { #if !UNITY_EDITOR _registerAPN(); #endif } /// /// 判斷當前是否開始了授權權限 /// /// public bool HaveNoticeNotifocation() { #if !UNITY_EDITOR Debug.Log("判斷當前是否開始了授權權限"+_haveNoticeNotifocation()); return _haveNoticeNotifocation(); #endif return true; } /// /// 通知授權回調 /// /// 結果 public void GetpermissionCallBack(string ret) { //授權成功后重新添加首次的本地通知 AddLocalNotice(titleStr,subtitleStr,contentStr,time,identifier); } /// /// 判斷是否注冊了通知權限回調 /// /// public void CheckPermissionCallBack(string ret) { Debug.Log("判斷是否注冊了通知權限回調..." + ret); } /// /// 添加一條推送 /// /// 標題 /// 副標題 /// 內容 /// 時間(多少秒之后) /// 標識符,用于移除單條推送 public void AddLocalNotice(string titleStr, string subtitleStr, string contentStr, int time, string identifier) { if (HaveNoticeNotifocation()) { #if !UNITY_EDITOR if (time>0) { _addLocalNotice(titleStr, subtitleStr, contentStr, time, identifier); } #endif } else { this.titleStr = titleStr; this.subtitleStr = subtitleStr; this.contentStr = contentStr; this.time = time; this.identifier = identifier; _checkPermission(); } } /// /// 移除單個推送 /// /// 標識符 public void RemoveOneNotificationWithID(string noticeId) { #if !UNITY_EDITOR _removeOneNotificationWithID(noticeId); #endif } /// /// 移除所有推送 /// public void RemoveAllNotification() { #if !UNITY_EDITOR _removeAllNotification(); #endif } /// /// 顯示提示信息 /// /// 標題 /// 顯示文本 /// 取消 /// 點擊OK按鈕內容 public void ShowSettingAlert(string title,string content,string cancelTxt,string okTxt) { _showSettingAlert(title, content, cancelTxt, okTxt); } #endif }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
創建LocalNotificationTest.cs 放到Scripts文件夾下,此腳本是處理Unity通過按鈕點擊調用的本地推送和移除推送邏輯:
using UnityEngine; using UnityEngine.UI; public class LocalNotificationTest : MonoBehaviour { /// /// 推送標簽 -- 可根據這個標簽關閉未推送消息 /// private readonly string pushLabelString = "本地推送標簽"; /// /// 推送時間 -- 60表示60秒后推送消息 /// private int pushTime = 60; // 開啟推送按鈕 public Button StartPushBtn; // 關閉推送按鈕 public Button StopPushBtn; void Start() { StartPushBtn.onClick.AddListener(() => { PushSwitch(true); }); StopPushBtn.onClick.AddListener(() => { PushSwitch(false); }); } void PushSwitch(bool isOn) { #if UNITY_EDITOR Debug.Log("PushSwitch 滿體力值推送..." + isOn); #elif UNITY_IOS if (isOn) { if(!LocalNotification_IOS.Instance.HaveNoticeNotifocation()) { string title = "提示"; string content = "功能不可用,請在設置->通知->游戲->設置允許通知"; string cancelTxt = "取消"; string okTxt = "去設置"; LocalNotification_IOS.Instance.ShowSettingAlert(title, content, cancelTxt, okTxt); } else { SetPushContext(); } } else { LocalNotification_IOS.Instance.RemoveOneNotificationWithID(pushLabelString); } #endif } // 設置推送內容 void SetPushContext() { #if UNITY_EDITOR Debug.Log("推送內容設置,發起定時推送"); #elif UNITY_IOS // 推送前關閉同標簽未推送消息 // LocalNotification_IOS.Instance.RemoveOneNotificationWithID(pushLabelString); // 設置推送 LocalNotification_IOS.Instance.AddLocalNotice("應用名稱", "本地推送標題!","本地推送內容...", pushTime, pushLabelString); #endif } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
所有代碼在工程下的目錄:
這是所有代碼網盤鏈接, 密碼: b7jm
源碼分享在文末哦~ 別著急,先按照步驟看完吧。
四,測試場景搭建
場景搭建賦值步驟如下:
創建兩個按鈕,修改文本內容和顯示位置調整
創建一個空物體并掛載腳本LocalNotification_IOS和LocalNotificationTest腳本
將兩個按鈕拖拽賦值給LocalNotificationTest
整體程序結構如下圖:
五,處理自動配置
創建腳本IOSAggregateEditor.cs 放到Editor文件夾下, 此腳本為編輯器腳本,只處理IOS打包自動配置:
using System.IO; using UnityEditor; using UnityEngine; #if UNITY_IOS using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; #endif /// /// IOS打包自動配置文件 /// public class IOSAggregateEditor { #if UNITY_IOS [PostProcessBuildAttribute(100)] public static void onPostProcessBuild(BuildTarget target, string targetPath) { if (target != BuildTarget.iOS) { return; } string projPath = PBXProject.GetPBXProjectPath(targetPath); PBXProject proj = new PBXProject(); proj.ReadFromString(File.ReadAllText(projPath)); string unityTarget = proj.GetUnityFrameworkTargetGuid(); //*******************************添加framework*******************************// proj.AddFrameworkToProject(unityTarget, "UserNotifications.framework", false); string content = proj.WriteToString(); File.WriteAllText(projPath, content); } #endif }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
六,雙端打包測試
Unity打包設置:
設置公司名稱,項目名稱(隨便起,只要IOS開發者賬號允許打包即可)

通過 “File” --> “Build Setting…” 打開Build Setting彈窗,如下圖:
選擇 “IOS” 后,點擊右下角 “Switch Platform” 按鈕,切換到IOS平臺,如下圖:
等待進度條完成即可。
切換平臺后點擊 “Add Open Scenes” 添加當前場景到Scenes In Build,最后點擊右下角 “Build” 按鈕導出Xcode工程,如下圖:
然后命名為 “IOSLocalPushXcode” 指定到當前工程目錄,選擇 “Save” 導出Xcode工程:
Xcode打包設置:
Xcode工程目錄如下,然后雙擊 ”Unity-iPhone.xcodeproj“ 打開Xcode工程:
打開Xcode后,左側選擇 “Unity-iPhone”, 上面選擇 ”Signing & Capabilities“,然后選擇Team。
最后連線選擇設備,點擊運行,等待進度條完成,自動安裝成功即可:
七,查看測試結果
打開程序點擊 “開啟本地推送按鈕” ,
首次點擊開啟推送按鈕會出現提示 —> 選擇 “去設置” —> 然后選擇 “允許” 消息推送
設置允許后,后續點擊開啟推送按鈕則不會進行此提示,會默認開始設置推送。
允許后,將退出程序(關閉進程),等待設置的推送時間后,在IOS上會顯示如下推送消息:
【注意點擊推送后一定要關閉程序,程序在線是不會進行推送的】
上圖可以看到,我們在代碼中設置的標題,內容等屬性都可以在通知中顯示出來,大功告成。
標題說好的文末源碼
怎么樣?此篇文章對你有幫助嗎?若有幫助麻煩支持下;若有問題歡迎留言討論。
Android iOS unity
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。