【问题标题】:Notification clicked Callback通知点击回调
【发布时间】:2016-12-09 11:19:47
【问题描述】:

我正在尝试为 Unity3D 编写一个插件,以便在用户单击本地时获取回调或在应用程序关闭时推送通知。我为此尝试了 Prime31 Etcetera Plugin,但它不起作用。所以我决定编写自己的插件。我尝试了几种方法,但没有奏效。谁能指导我怎么做?

【问题讨论】:

    标签: android unity3d push-notification android-notifications


    【解决方案1】:

    几天前我只需要这样做。这是我使用的类:

    using System;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.iOS;
    using System.Collections;
    
    public class Notifications : MonoBehaviour
    {
        private void Awake()
        {
            // If app launched for the first time, set up a notification
            if (!PlayerPrefs.HasKey("firstLaunch"))
            {
                UnityEngine.iOS.NotificationServices.ClearLocalNotifications();
                UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications();
    
                PlayerPrefs.SetInt("firstLaunch", 0);
    
                UnityEngine.iOS.NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);
    
                var notification = new UnityEngine.iOS.LocalNotification();
                notification.alertAction = "Title of the notification";
                notification.alertBody = "Body of the notification";
                // I set this to -1 because I didn't want to have the badge on the app icon
                notification.applicationIconBadgeNumber = -1;
    
                var currentDate = DateTime.Now;
                var targetDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 9, 15, 00, DateTimeKind.Local);
    
                targetDate = (currentDate < targetDate) ? targetDate : targetDate.AddDays(1);
    
                // Set the date and time the notification will fire
                notification.fireDate = targetDate;
                // In my case in wanted to repeat it everyday at 9:15 AM
                notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;
    
                UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(notification);
            }
        }
    
        private void OnApplicationPause(bool state)
        {
            // If app returns to foreground
            if (!state)
            {
                // If this count > 0, then a notification has been clicked
                if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0)
                {
                    // Do stuff
                }
    
                UnityEngine.iOS.NotificationServices.ClearLocalNotifications();
            }
    
        }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 这是为 iOS 提供的,Unity 为 iOS 提供通知服务,但不为 android 提供通知服务。我怎样才能在android中实现它?
    • 糟糕,我没有注意标签。我从来没有为 Android 做过,抱歉。也许你可以试试这个插件,它是免费的! (github.com/Agasper/unity-android-notifications)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多