基礎教你Unity接入IOS原生本地推送

      網友投稿 1124 2025-03-31

      一,新建Unity項目

      零基礎教你Unity接入IOS原生本地推送

      打開Unity Hub,點擊 ”新建“ , 輸入項目名稱,選擇存儲位置,點擊創建即可。

      創建后Unity會自動打開,我們先創建幾個文件夾

      IOSLocalNotification: 總目錄,存儲本次實例使用的所有相關文件

      Editor: 二級文件夾,存儲打包Xcode的自動配置腳本

      Plugins: 存儲對接IOS的橋接文件

      Scenes: 存儲場景,直接把創建項目的帶進來就行了

      Scripts: 存儲項目Unity的.cs腳本

      修改場景名稱為 ”LocalNotification“, 然后"場景從外部修改了" 我們點擊 ”Reload“ 重新加載下就可以了。

      至此創建Unity項目和準備工作完成。

      二,梳理程序流程

      點擊按鈕觸發本地推送

      代碼調用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]; } }

      在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(); } }

      這樣寫就實現了,從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 }

      創建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 } }

      所有代碼在工程下的目錄:

      這是所有代碼網盤鏈接, 密碼: 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 }

      六,雙端打包測試

      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小時內刪除侵權內容。

      上一篇:excel表格公式減法怎樣使用(excel表格減法公式怎么操作的)
      下一篇:如何快速找到另外一張表格內的單元格??(表格中查找的數據怎么弄到另一個表格中去)
      相關文章
      亚洲一区二区观看播放| 亚洲国语精品自产拍在线观看| 亚洲AV日韩AV高潮无码专区| 亚洲综合色婷婷七月丁香| 偷自拍亚洲视频在线观看99| 亚洲精品第一国产综合亚AV| 亚洲精华液一二三产区| 亚洲熟妇无码八V在线播放| 97久久国产亚洲精品超碰热| 国产成人精品日本亚洲专| 亚洲一区欧洲一区| 亚洲日韩国产欧美一区二区三区| 国产午夜亚洲精品| 亚洲乱码av中文一区二区| 亚洲欧美综合精品成人导航| 亚洲欧美综合精品成人导航| 亚洲国产精品日韩av不卡在线| 日本亚洲欧美色视频在线播放 | 亚洲精品国产字幕久久不卡 | 久久狠狠爱亚洲综合影院 | 国产亚洲美女精品久久久2020| 国产性爱在线观看亚洲黄色一级片| 国产亚洲精品免费| 亚洲美女高清一区二区三区| 中文字幕亚洲专区| 亚洲AV无码精品色午夜在线观看| 亚洲AV无码成人精品区天堂| 久久久久久久亚洲Av无码| 久久久久亚洲AV无码麻豆| 亚洲一区二区三区亚瑟| 亚洲熟妇无码av另类vr影视| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲亚洲人成综合网络| 亚洲国产精品无码中文字| 亚洲国产精品自在线一区二区 | 在线观看亚洲精品专区| 久久久亚洲精品蜜桃臀 | 亚洲色大成WWW亚洲女子| 小说区亚洲自拍另类| 在线观看亚洲成人| 亚洲激情中文字幕|