unity android 远程推送,Unity本地通知(iOS和Android)

本文详细介绍了在Unity中实现本地通知的方法,包括针对iOS和Android平台的代码实现。同时,提到了在Android平台上遇到的'Unable to merge android manifests'错误,并给出了修改AndroidManifest.xml为'debuggable=false'的解决方案,确保本地通知插件的正确集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

#if UNITY_IPHONE

using NotificationServices = UnityEngine.iOS.NotificationServices;

using NotificationType = UnityEngine.iOS.NotificationType;

#endif

public class MyLocalNotification : MonoBehaviour

{

//本地推送

public static void NotificationMessage(string message, int hour, bool isRepeatDay)

{

int year = System.DateTime.Now.Year;

int month = System.DateTime.Now.Month;

int day = System.DateTime.Now.Day;

System.DateTime newDate = new System.DateTime(year, month, day, hour, 0, 0);

NotificationMessage(message, newDate, isRepeatDay);

}

//本地推送 你可以传入一个固定的推送时间

public static void NotificationMessage(string message, System.DateTime newDate, bool isRepeatDay)

{

#if UNITY_IPHONE

//推送时间需要大于当前时间

if(newDate > System.DateTime.Now)

{

UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();

localNotification.fireDate =newDate;

localNotification.alertBody = message;

localNotification.applicationIconBadgeNumber = 1;

localNotification.hasAction = true;

localNotification.alertAction = "这是notificationtest的标题";

if(isRepeatDay)

{

//是否每天定期循环

localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.ChineseCalendar;

localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;

}

localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName;

UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);

}

#endif

#if UNITY_ANDROID

if (newDate > System.DateTime.Now)

{

LocalNotification.SendNotification(1,10,"这是notificationtest的标题","这是notificationtest的消息",new Color32(0xff, 0x44, 0x44, 255));

if (System.DateTime.Now.Hour >= 12) {

//System.DateTime dataTimeNextNotify = new System.DateTime(

long delay = 24 * 60 * 60 - ((System.DateTime.Now.Hour - 12)* 60 * 60 + System.DateTime.Now.Minute * 60 + System.DateTime.Now.Second);

LocalNotification.SendRepeatingNotification(2,delay, 24 * 60 * 60,"这是notificationtest的标题","每天中午12点推送",new Color32(0xff, 0x44, 0x44, 255));

}

else

{

long delay = (12 - System.DateTime.Now.Hour)* 60 * 60 - System.DateTime.Now.Minute * 60 - System.DateTime.Now.Second;

LocalNotification.SendRepeatingNotification(2,delay,24 * 60 * 60 ,"这是notificationtest的标题","每天中午12点推送",new Color32(0xff, 0x44, 0x44, 255));

}

}

#endif

}

void Awake()

{

#if UNITY_IPHONE

UnityEngine.iOS.NotificationServices.RegisterForNotifications (

NotificationType.Alert |

NotificationType.Badge |

NotificationType.Sound);

#endif

//第一次进入游戏的时候清空,有可能用户自己把游戏冲后台杀死,这里强制清空

CleanNotification();

}

void OnApplicationPause(bool paused)

{

//程序进入后台时

if (paused)

{

//10秒后发送

NotificationMessage("这是notificationtest的推送正文信息", System.DateTime.Now.AddSeconds(10), false);

//每天中午12点推送

NotificationMessage("每天中午12点推送", 12, true);

}

else

{

//程序从后台进入前台时

CleanNotification();

}

}

//清空所有本地消息

void CleanNotification()

{

#if UNITY_IPHONE

UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification ();

l.applicationIconBadgeNumber = -1;

UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (l);

UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();

UnityEngine.iOS.NotificationServices.ClearLocalNotifications ();

#endif

#if UNITY_ANDROID

LocalNotification.CancelNotification(1);

LocalNotification.CancelNotification(2);

#endif

}

}

Android:Unable to merge android manifests

集成安卓本地通知插件报错

android-notifications

解决:修改AndroidManifest.xml debuggable="false"

### Unity IAP Server-to-Server Notification 实现与透传机制 Unity IAP 的服务器到服务器通知(S2S)是一种安全的方式,用于验证用户的购买行为并将其记录在开发者自己的服务器上。这种机制通过发送 HTTP POST 请求来告知开发者的服务器有关成功的交易详情。 以下是关于 Unity IAP S2S 通知的核心概念及其实现方式: #### 核心原理 当用户完成一次应用内购买时,Unity IAP 不仅会向客户端返回确认消息,还会触发一个后台请求至开发者指定的 URL 地址。此地址应由开发者配置于 Unity Services 中的相关设置页面下[^1]。该请求携带了经过加密签名的数据包,其中包含了订单的具体信息以及必要的元数据以便进一步处理。 #### 数据结构说明 接收到的通知通常包括以下几个重要字段: - `transactionId`: 唯一标识此次交易的身份码。 - `productId`: 被购商品的产品ID,在商店平台定义的商品唯一标志符。 - `purchaseToken` 或者其他形式的安全令牌:用来帮助再次校验收据的真实性。 - 数字签名以及其他辅助参数等附加属性也可能存在以增强安全性。 #### 安全性考量 为了防止伪造请求或者篡改数据的情况发生,所有来自 Unity 的 S2S Notifications 都会被加上 HMAC-SHA256 类型的消息认证码(MAC),它基于预设的秘密密钥生成。接收端需要利用相同的密钥重新计算MAC并与原值对比从而判断来源合法性。 下面展示了一个简单的 PHP 示例脚本片段如何解析这些输入并执行基本验证逻辑: ```php <?php // 获取POST过来的内容 $jsonData = file_get_contents('php://input'); $data = json_decode($jsonData, true); if (!$data || !isset($data['signature']) || !isset($data['payload'])) { http_response_code(400); exit("Invalid request"); } $secretKey = 'your_secret_key_here'; // 替换为实际使用的秘钥 $calculatedSignature = hash_hmac('sha256', $data['payload'], $secretKey); if ($calculatedSignature !== $data['signature']) { http_response_code(403); exit("Security check failed"); } // 此处可以继续业务流程... ?> ``` 上述代码展示了初步的数据提取过程及HMAC检验步骤。如果一切正常,则可以根据 payload 内部所含的信息更新数据库状态或者其他操作。 #### 注意事项 - 开发人员应当妥善保管 secret key 并且绝不在公共场合暴露出来。 - 对任何异常情况都要有良好的错误捕捉日志记录能力,方便后续排查问题所在。 - 应考虑网络延迟等因素影响下的重复提交可能性,并设计去重策略避免多次计入相同订单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值