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

阅读: 评论:0

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

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

零基础教你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端代码的编写,显然超出了本文的范畴(我也不会)。其实我们只要知道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/// <summary>/// 移除所有推送/// </summary>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/// <summary>
/// 本地推送IOS -- 调用桥接文件
/// </summary>
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);/// <summary>/// 注册APN推送通道/// </summary>private void RegisterAPN(){
#if !UNITY_EDITOR_registerAPN();
#endif}/// <summary>/// 判断当前是否开始了授权权限/// </summary>/// <returns></returns>public bool HaveNoticeNotifocation(){
#if !UNITY_EDITORDebug.Log("判断当前是否开始了授权权限"+_haveNoticeNotifocation());return _haveNoticeNotifocation();
#endifreturn true;}/// <summary>/// 通知授权回调/// </summary>/// <param name="ret">结果</param>public void GetpermissionCallBack(string ret){//授权成功后重新添加首次的本地通知AddLocalNotice(titleStr,subtitleStr,contentStr,time,identifier);}/// <summary>/// 判断是否注册了通知权限回调/// </summary>/// <param name="ret"></param>public void CheckPermissionCallBack(string ret){Debug.Log("判断是否注册了通知权限回调..." + ret);}/// <summary>/// 添加一条推送/// </summary>/// <param name="titleStr">标题</param>/// <param name="subtitleStr">副标题</param>/// <param name="contentStr">内容</param>/// <param name="time">时间(多少秒之后)</param>/// <param name="identifier">标识符,用于移除单条推送</param>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 = tStr = contentStr;this.time = time;this.identifier = identifier;_checkPermission();            }}/// <summary>/// 移除单个推送/// </summary>/// <param name="noticeId">标识符</param>public void RemoveOneNotificationWithID(string noticeId){
#if !UNITY_EDITOR_removeOneNotificationWithID(noticeId);
#endif}/// <summary>/// 移除所有推送/// </summary>public void RemoveAllNotification(){
#if !UNITY_EDITOR_removeAllNotification();
#endif}/// <summary>/// 显示提示信息/// </summary>/// <param name="title">标题</param>/// <param name="content">显示文本</param>/// <param name="cancelTxt">取消</param>/// <param name="okTxt">点击OK按钮内容</param>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
{/// <summary>/// 推送标签 -- 可根据这个标签关闭未推送消息/// </summary>private readonly string pushLabelString = "本地推送标签";/// <summary>/// 推送时间 -- 60表示60秒后推送消息/// </summary>private int pushTime = 60;// 开启推送按钮public Button StartPushBtn;// 关闭推送按钮public Button StopPushBtn;void Start(){Click.AddListener(() => { PushSwitch(true); });Click.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

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


四,测试场景搭建

场景搭建赋值步骤如下:

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

整体程序结构如下图:


五,处理自动配置

创建脚本IOSAggregateEditor.cs 放到Editor文件夹下, 此脚本为编辑器脚本,只处理IOS打包自动配置:

using System.IO;
using UnityEditor;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
#endif/// <summary>
/// IOS打包自动配置文件
/// </summary>
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工程目录如下,然后双击 ”deproj“ 打开Xcode工程:

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

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


七,查看测试结果

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

首次点击开启推送按钮会出现提示 —> 选择 “去设置” —> 然后选择 “允许” 消息推送
设置允许后,后续点击开启推送按钮则不会进行此提示,会默认开始设置推送。

允许后,将退出程序(关闭进程),等待设置的推送时间后,在IOS上会显示如下推送消息:
【注意点击推送后一定要关闭程序,程序在线是不会进行推送的】

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

标题说好的文末源码

没有积分的童鞋关注文末公众号,回复【IOS友盟】获取完整源码吧~

怎么样?此篇文章对你有帮助吗?若有帮助麻烦点赞支持下;若有问题欢迎留言讨论。

本文发布于:2024-02-04 23:02:59,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170718396560555.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:教你   源码   基础   Unity   IOS
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23