2

Я пытаюсь написать плагин для Unity3D, чтобы получить обратный вызов, когда пользователь нажимает на локальное или push-уведомление, когда приложение закрыто. Я попробовал Prime31 Etcetera Plugin для этого, но он не работает. Поэтому я решил написать свой собственный плагин. Я попробовал несколько методов, но это не сработало. Может ли кто-нибудь вести меня, как это сделать?Уведомление щелкнуло Обратный звонок

ответ

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(); 
     } 

    } 
} 

Надеюсь, это поможет!

+0

Это для iOS, Unity предоставляет службу уведомлений для iOS, но не для Android. Как я могу достичь этого в андроиде? –

+0

К сожалению, я не обращал внимания на теги. Я никогда не делал этого для Android, извините. Может быть, вы можете попробовать этот плагин, это бесплатно! (Https://github.com/Agasper/unity-android-notifications) – Whatever

Смежные вопросы