首页 >> 大全

零基础教你Unity接入IOS原生本地推送 【文末源码】

2023-12-02 大全 25 作者:考证青年

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

从新建项目开始的保姆级教程,教你Unity接入IOS原生本地推送。

一,新建Unity项目

打开Unity Hub,点击 ”新建“ , 输入项目名称,选择存储位置,点击创建即可。

创建后Unity会自动打开,我们先创建几个文件夹

修改场景名称为 ”“, 然后"场景从外部修改了" 我们点击 ”“ 重新加载下就可以了。

至此创建Unity项目和准备工作完成。

二,梳理程序流程 三,处理代码逻辑

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;
#endifpublic class LocalNotification_IOS : MonoBehaviour
{#if UNITY_IOS// 移除所有推送 -- 调用到IOS[DllImport("__Internal")]private static extern void _removeAllNotification();   
#endif/// /// 移除所有推送/// public void RemoveAllNotification(){_removeAllNotification();}
}

这样写就实现了,从Unity调用到IOS了。

流程: Unity按钮点击触发n()方法 --> 方法体调用 on() 通过[("")]关联到IOS --> 调用到IOS中的 (void)n。

由于篇幅问题,IOS端完整代码我就不贴出来了,需要的童鞋点击本段末链接下载即可。

创建S.cs 放到文件夹下,此脚本处理对接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;#endregionprivate void Awake(){Instance = this;// 切换场景不销毁DontDestroyOnLoad(this.gameObject);// 移除所有推送#if !UNITY_EDITORRemoveAllNotification();#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_EDITORDebug.Log("判断当前是否开始了授权权限"+_haveNoticeNotifocation());return _haveNoticeNotifocation();
#endifreturn 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_EDITORif (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
}

创建t.cs 放到文件夹下,此脚本是处理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_EDITORDebug.Log("PushSwitch 满体力值推送..." + isOn);
#elif UNITY_IOSif (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_EDITORDebug.Log("推送内容设置,发起定时推送");
#elif UNITY_IOS// 推送前关闭同标签未推送消息// LocalNotification_IOS.Instance.RemoveOneNotificationWithID(pushLabelString);// 设置推送LocalNotification_IOS.Instance.AddLocalNotice("应用名称", "本地推送标题!","本地推送内容...", pushTime, pushLabelString);
#endif}}

所有代码在工程下的目录:

这是所有代码网盘链接, 密码: b7jm

源码分享在文末哦~ 别着急,先按照步骤看完吧。

四,测试场景搭建

场景搭建赋值步骤如下:

创建两个按钮,修改文本内容和显示位置调整创建一个空物体并挂载脚本S和t脚本将两个按钮拖拽赋值给t

整体程序结构如下图

五,处理自动配置

创建脚本.cs 放到文件夹下, 此脚本为编辑器脚本,只处理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
}

六,双端打包测试

设置公司名称,项目名称(随便起,只要IOS开发者账号允许打包即可)

通过 “File” --> “Build …” 打开Build 弹窗,如下图:

选择 “IOS” 后,点击右下角 “ ” 按钮,切换到IOS平台,如下图:

_源码都怎么上传使用_原生源码是什么意思

等待进度条完成即可。

切换平台后点击 “Add Open ” 添加当前场景到 In Build,最后点击右下角 “Build” 按钮导出Xcode工程,如下图:

然后命名为 “” 指定到当前工程目录,选择 “Save” 导出Xcode工程:

Xcode工程目录如下,然后双击 ”Unity-.“ 打开Xcode工程:

打开Xcode后,左侧选择 “Unity-”, 上面选择 ” & “,然后选择Team。

最后连线选择设备,点击运行,等待进度条完成,自动安装成功即可:

七,查看测试结果

打开程序点击 “开启本地推送按钮” ,

首次点击开启推送按钮会出现提示 —> 选择 “去设置” —> 然后选择 “允许” 消息推送

设置允许后,后续点击开启推送按钮则不会进行此提示,会默认开始设置推送。

允许后,将退出程序(关闭进程),等待设置的推送时间后,在IOS上会显示如下推送消息:

【注意点击推送后一定要关闭程序,程序在线是不会进行推送的】

上图可以看到,我们在代码中设置的标题,内容等属性都可以在通知中显示出来,大功告成。

标题说好的文末源码

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了