unity3dnotificationandroid,使用UnityMobile通知包在手。。。

unity3dnotificationandroid,使用UnityMobile通知包在手。。。

2023年6月27日发(作者:)

unity3dnotificationandroid,使⽤UnityMobile通知包在⼿。。。运⾏时API分为两部分:AndroidNotificationCenter和iOSNotificationCenter。这些可⽤于计划和管理其各⾃平台的通知。您可以从我们的GitHub页⾯下载⼀个NotificationsSamples⽰例项⽬,该项⽬实现了⼀个⾼级封装,可⽤于使⽤相同的API向Android和iOS发送通知,或查看下⾯的代码⽰例。该软件包⽀持以下功能:计划安排本地⼀次或可重复的通知。取消已经显⽰的和即将发⽣的(计划的)通知。安卓:在Android Oreo及更⾼版本上创建和修改通知渠道(类别)。设备重新启动时保留通知。设置⾃定义通知图标。iOS:使⽤Apple Push Notification Service(APN)接收远程通知。如果设备在您的应⽤程序运⾏时收到来⾃其他应⽤程序的通知,请修改远程通知内容。将通知分组为线程(仅在iOS 12+上受⽀持)。要求:⽀持Android 4.4(API 19)和iOS 10或更⾼版本。与Unity 2018.3或更⾼版本兼容。安卓建⽴通知频道每个本地通知必须属于⼀个通知通道。通知通道仅在Android 8.0 Oreo及更⾼版本上受⽀持。在较旧的Android版本上,此程序包模拟通知通道的⾏为。Importance为通知频道设置的优先级(重要)之类的设置适⽤于单个通知,即使在8.0之前的Android版本上也是如此。var c = new AndroidNotificationChannel(){Id = "channel_id",Name = "Default Channel",Importance = ,Description = "Generic notifications",};erNotificationChannel(c);发送⼀个简单的通知本⽰例说明如何安排简单的⽂本通知并将其发送到上⼀步中创建的通知通道。var notification = new AndroidNotification(); = "SomeTitle"; = "SomeText";me = utes(5);tification(notification, "channel_id");如果您没有为每个通知指定⾃定义图标,则默认的Unity图标显⽰在状态栏中。您可以在“项⽬设置” **窗⼝(菜单:**“编辑” >“项⽬设置” >“移动通知设置”)中配置通知图标。每当您在脚本中安排通知时,请使⽤在“移动通知设置”窗⼝中定义的图标ID 。con = "my_custom_icon_id";您可以选择设置⼀个⼤图标,该图标也会显⽰在通知视图中。较⼩的图标显⽰为⼤图标上⽅的⼩徽章。con = "my_custom_large_icon_id";安排通知后,Unity会为每个通知分配⼀个唯⼀的标识符。您可以使⽤标识符来跟踪通知状态或取消通知状态。通知状态跟踪仅适⽤于Android 6.0棉花糖及更⾼版本。var identifier = tification(n, "channel_id");使⽤以下代码⽰例检查您的应⽤程序是否已将通知传递到设备,并根据结果执⾏任何操作。if ( cheduledNotificationStatus(identifier) == led){// Replace the currently scheduled notification with a new ScheduledNotification(identifier, newNotification, channel);}else if ( cheduledNotificationStatus(identifier) == red){//Remove the notification from the status Notification(identifier);}else if ( cheduledNotificationStatus(identifier) == n){tification(newNotification, "channel_id");}设备重启后保留预定的通知默认情况下,应⽤在设备重新启动时会删除计划的通知。要在⽤户重新打开设备电源时⾃动重新计划所有通知,请在“项⽬设置”窗⼝(菜单:“编辑” >“项⽬设置” >“移动通知设置”)中启⽤“重新启动设备时重新计划通知”设置。这会将权限添加到您的应⽤清单中。RECEIVE_BOOT_COMPLETED在应⽤程序运⾏时处理收到的通知您可以订阅ficationReceived事件以在应⽤程序运⾏时每当设备收到远程通知时接收回调。cationReceivedCallback receivedNotificationHandler =delegate(AndroidNotificationIntentData data){var msg = "Notification received : " + + "n";msg += "n Notification received: ";msg += "n .Title: " + ;msg += "n .Body: " + ;msg += "n .Channel: " + l;(msg);};ficationReceived += receivedNotificationHandler;保存⾃定义数据并在⽤户从通知中打开应⽤程序时进⾏检索要在通知对象中存储任意字符串数据,请设置IntentData属性。var notification = new AndroidNotification();Data = "{"title": "Notification 1", "data": "200"}";tification(notification, "channel_id");如果⽤户从通知中打开该应⽤程序,则可以按以下⽅式检索该应⽤程序中的任何内容以及它分配给它的任何数据:var notificationIntentData = tNotificationIntent();if (notificationIntentData != null){var id = ;var channel = l;var notification = cation;}如果以其他任何⽅式打开应⽤程序,则GetLastNotificationIntent返回null。iOS请求授权您需要从系统请求权限才能发布本地通知并接收远程通知。如果您打算在⽤户确认授权请求后向其发送远程通知,则需要检索DeviceToken。为此,必须创建请求并将其registerForRemoteNotifications设置为true。有关如何向设备发送推送通知以及如何向应⽤程序添加推送通知⽀持的更多信息,请参阅Apple Developer⽹站⽂档。(可选)您可以请求⽤户许可以仅发送某些通知类型。以下⽰例显⽰了如何请求权限以显⽰“ UI警报”对话框并在应⽤程序图标上添加徽章。但是,⽤户可以随时在设置应⽤中更改每种通知类型的授权状态,因此要检查实际的授权状态,请致电ificationSettings。或者,您可以在“项⽬设置”窗⼝(菜单:“编辑” >“项⽬设置” >“移动通知设置”)中启⽤“应⽤程序启动时的请求授权”设置,这将使应⽤程序在以下情况下⾃动显⽰权限请求对话框:⽤户启动该应⽤程序。之后,您可以再次调⽤此⽅法以确定当前的授权状态。如果⽤户已经授予或拒绝了授权,则权限请求对话框将不会再次显⽰。IEnumerator RequestAuthorization(){using (var req = new AuthorizationRequest( | , true)){while (!shed){yield return null;};string res = "n RequestAuthorization: n";res += "n finished: " + shed;res += "n granted : " + d;res += "n error: " + ;res += "n deviceToken: " + Token;(res);}发送⼀个简单的通知var timeTrigger = new iOSNotificationTimeIntervalTrigger(){TimeInterval = new TimeSpan(0, minutes, seconds),Repeats = false};var notification = new iOSNotification(){// You can optionally specify a custom identifier which can later be// used to cancel the notification, if you don't set one, a unique// string will be generated fier = "_notification_01",Title = "Title",Body = "Scheduled at: " + tDateString() + " triggered in 5 seconds",Subtitle = "This is a subtitle, something, ",ShowInForeground = true,ForegroundPresentationOption = ( | ),CategoryIdentifier = "category_a",ThreadIdentifier = "thread1",Trigger = timeTrigger,};leNotification(notification);以下代码⽰例在未触发的情况下取消通知:ScheduledNotification(fier);以下代码⽰例如果通知已经显⽰给⽤户,则将其从通知中⼼中删除:DeliveredNotification(fier);其他触发因素除了时间间隔触发器外,您还可以使⽤⽇历和位置触发器。中的所有字段iOSNotificationCalendarTrigger都是可选的,但是您需要⾄少设置⼀个字段才能使触发器起作⽤。例如,如果仅设置⼩时和分钟字段,则系统会在下⼀个指定的⼩时和分钟上⾃动触发通知。var calendarTrigger = new iOSNotificationCalendarTrigger(){// Year = 2018,// Month = 8,//Day = 30,Hour = 12,Minute = 0,// Second = 0Repeats = false};如果要安排在设备进⼊或离开特定地理区域时发送通知的时间,还可以创建位置触发器。在使⽤此触发器安排任何通知之前,您的应⽤必须具有使⽤“核⼼位置”的权限,并且必须具有“使⽤时”权限。使⽤Unity LocationService API请求此授权。有关更多信息,请参阅Apple Developer⽹站上的Core Location⽂档。在此⽰例中,中⼼坐标是使⽤WGS 84系统定义的。当⽤户进⼊巴黎埃菲尔铁塔周围250⽶半径范围内的区域时,该应⽤会触发通知。var locationTrigger = new iOSNotificationLocationTrigger(){Center = new Vector2(2.294498f, 48.858263f),Radius = 250f,NotifyOnEntry = true,NotifyOnExit = false,}在应⽤运⾏时处理收到的通知如果您的应⽤在运⾏时触发了通知,则您可以执⾏⾃定义操作,⽽不是显⽰通知警报。默认情况下,如果您的应⽤在前台运⾏时触发了本地通知,则设备不会为该通知显⽰警报。如果希望通知的⾏为就像设备未在运⾏应⽤程序⼀样,请ShowInForeground在安排通知时设置属性:Foreground = true;// In this case you need to specify its 'ForegroundPresentationOption'oundPresentationOption = ( | );另外,您可以在应⽤触发通知时执⾏其他操作。例如,您可以使⽤应⽤程序的UI显⽰通知内容。为此,请订阅OnNotificationReceived活动。每当收到本地或远程通知时,⽆论它是否显⽰在前台,您的应⽤都会调⽤此事件。要修改或隐藏您的应⽤在运⾏时收到的远程通知的内容,请订阅该OnRemoteNotificationReceived事件。如果执⾏此操作,则在您的应⽤程序运⾏时不会显⽰远程通知。如果您仍然想要显⽰警报,请使⽤远程通知的内容安排本地通知,如下所⽰:teNotificationReceived += notification =>{// When a remote notification is received, modify its contents and show it// after 1 timeTrigger = new iOSNotificationTimeIntervalTrigger(){TimeInterval = new TimeSpan(0, 0, 1),Repeats = false};iOSNotification n = new iOSNotification(){Title = "Remote : " + ,Body = "Remote : " + ,Subtitle = "Remote: " + le,ShowInForeground = true,ForegroundPresentationOption = | | ,CategoryIdentifier = ryIdentifier,ThreadIdentifier = Identifier,Trigger = timeTrigger,};leNotification(n);("Rescheduled remote notifications with id: " + fier);};保存⾃定义数据并在⽤户从通知中打开应⽤程序时进⾏检索要将任意字符串数据存储在通知对象中,请设置Data属性:var notification = new iOSNotification(); = "{"title": "Notification 1", "data": "200"}";//. other fields..leNotification(notification);以下代码⽰例显⽰了如何检索应⽤程序收到的最新通知:var n = tRespondedNotification();if (n != null){var msg = "Last Received Notification : " + fier + "n";msg += "n - Notification received: ";msg += "n - .Title: " + ;msg += "n - .Badge: " + ;msg += "n - .Body: " + ;msg += "n - .CategoryIdentifier: " + ryIdentifier;msg += "n - .Subtitle: " + le;msg += "n - .Data: " + ;(msg);}else{("No notifications received.");}如果⽤户从通知中打开应⽤程序,则GetLastRespondedNotification还返回该通知。否则,它返回null。常见问题为什么⼩Android图标在编辑器通知设置预览中为⽩⾊?⼩通知图标必须是单⾊的,Android会忽略图标图像中的所有⾮alpha通道,因此Unity会⾃动剥离所有RGB通道。您也可以通过将图标放在AssetsPluginsAndroidresdrawable-{scaleFactor}⽂件夹中来提供图标,在这种情况下,它们不会被⾃动处理,但是包含⾮Alpha通道的图标将⽆法在Android 5.0及更⾼版本上正确显⽰。可以通过设置属性来修改通知颜⾊。为什么当我的应⽤程序关闭且未在后台运⾏时,某些华为和⼩⽶⼿机上没有发送通知?似乎华为(包括荣耀)和⼩⽶采⽤了激进的节电技术 ,除⾮⽤户在设备设置中将该应⽤列⼊了⽩名单,否则它们会限制应⽤程序的后台活动。这意味着,如果应⽤已关闭或不在bacgkround中运⾏,则不会发送任何计划的通知。除了⿎励⽤户将您的应⽤列⼊⽩名单之外,我们没有任何其他⽅法可以解决此问题。如果带有位置触发器的通知不起作⽤,该怎么办?确保将CoreLocation框架添加到您的项⽬中。您可以在Unity编辑器的“移动通知设置”菜单中执⾏此操作(菜单:“编辑” >“项⽬设置” >“移动通知设置”)。或者,将其⼿动添加到Xcode项⽬中,或使⽤Unity Xcode API。您还需要使⽤位置服务API来请求您的应⽤访问位置数据的权限。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687842589a50038.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信