Skip to content

Instantly share code, notes, and snippets.

@yosun
Last active January 21, 2022 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosun/1e9285103e4614d37a763bc811df670a to your computer and use it in GitHub Desktop.
Save yosun/1e9285103e4614d37a763bc811df670a to your computer and use it in GitHub Desktop.
PushTester
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDuration</key>
<false/>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>Push Notify Me</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>${PRODUCT_BUNDLE_IDENTIFIER}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.1</string>
<key>CFBundleURLTypes</key>
<array/>
<key>CFBundleVersion</key>
<string>0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Location Nofications</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location Push</string>
<key>UILaunchStoryboardName~ipad</key>
<string>LaunchScreen-iPad</string>
<key>UILaunchStoryboardName~iphone</key>
<string>LaunchScreen-iPhone</string>
<key>UILaunchStoryboardName~ipod</key>
<string>LaunchScreen-iPhone</string>
<key>UIPrerenderedIcon</key>
<false/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UIRequiresFullScreen</key>
<true/>
<key>UIRequiresPersistentWiFi</key>
<false/>
<key>UIStatusBarHidden</key>
<true/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
<key>Unity_LoadingActivityIndicatorStyle</key>
<integer>-1</integer>
<key>UnityAddRemoteNotificationCapability</key>
<false/>
<key>UnityCloudProjectID</key>
<string></string>
<key>UnityCrashSubmissionURL</key>
<string></string>
<key>UnityNotificationDefaultAuthorizationOptions</key>
<integer>7</integer>
<key>UnityNotificationRequestAuthorizationForRemoteNotificationsOnAppLaunch</key>
<false/>
<key>UnityNotificationRequestAuthorizationOnAppLaunch</key>
<true/>
<key>UnityRemoteNotificationForegroundPresentationOptions</key>
<integer>-1</integer>
<key>UnityUseAPSReleaseEnvironment</key>
<false/>
<key>UnityUseLocationNotificationTrigger</key>
<false/>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
</dict>
</plist>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocationHelper : MonoBehaviour
{
public delegate void Vec2(Vector2 v);
public static Vec2 LocationFound;
// Start is called before the first frame update
void Start()
{
PollLatLng();
}
public static Vector2 latlng;
public void PollLatLng()
{
StartCoroutine(StartLoc());
}
IEnumerator StartLoc()
{
// First, check if user has location service enabled
/* if (!Input.location.isEnabledByUser)
yield break;*/
// Start service before querying location
Input.location.Start();
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
latlng = new Vector2(Input.location.lastData.longitude, Input.location.lastData.latitude);
LocationFound(latlng);
// Access granted and location value could be retrieved
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Notifications.iOS;
using System;
public class PushHelper : MonoBehaviour
{
static string deviceToken;
public delegate void VOID();
public delegate void VOIDstring(string s);
public static VOID PermissionGranted;
public static VOID PermissionDenied;
public enum TriggerTypes
{
Time,
Calendar,
Location,
Push
}
void Start ()
{
StartCoroutine(RequestAuthorization());
iOSNotificationCenter.OnRemoteNotificationReceived += remoteNotification =>
{
// When a remote notification is received, modify its contents and show it after 1 second.
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = new TimeSpan(0, 0, 1),
Repeats = false
};
iOSNotification notification = new iOSNotification()
{
Title = "Remote: " + remoteNotification.Title,
Body = "Remote: " + remoteNotification.Body,
Subtitle = "Remote: " + remoteNotification.Subtitle,
ShowInForeground = true,
ForegroundPresentationOption = PresentationOption.Sound | PresentationOption.Alert,
CategoryIdentifier = remoteNotification.CategoryIdentifier,
ThreadIdentifier = remoteNotification.ThreadIdentifier,
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
};
}
public static string GetLastNotification()
{
var notification = iOSNotificationCenter.GetLastRespondedNotification();
if (notification != null)
{
var msg = "Last Received Notification: " + notification.Identifier;
msg += "\n - Notification received: ";
msg += "\n - .Title: " + notification.Title;
msg += "\n - .Badge: " + notification.Badge;
msg += "\n - .Body: " + notification.Body;
msg += "\n - .CategoryIdentifier: " + notification.CategoryIdentifier;
msg += "\n - .Subtitle: " + notification.Subtitle;
msg += "\n - .Data: " + notification.Data;
Debug.Log(msg);
return notification.Identifier;
}
return null;
}
IEnumerator RequestAuthorization()
{
var authorizationOption = AuthorizationOption.Alert | AuthorizationOption.Badge;
using (var req = new AuthorizationRequest(authorizationOption, true))
{
while (!req.IsFinished)
{
yield return null;
};
string res = "\n RequestAuthorization:";
res += "\n finished: " + req.IsFinished;
res += "\n granted : " + req.Granted;
res += "\n error: " + req.Error;
res += "\n deviceToken: " + req.DeviceToken;
Debug.Log(res);
while(!req.IsFinished)yield return new WaitForSeconds(0.1f);
if (req.Granted)
{
deviceToken = req.DeviceToken;
PermissionGranted();
}
else
{
PermissionDenied();
}
}
}
public void CancelNotification( string identifier)
{
var notification = new iOSNotification()
{
Identifier = identifier
};
iOSNotificationCenter.RemoveScheduledNotification(notification.Identifier);
}
public void RemoveNotification( string identifier)
{
var notification = new iOSNotification()
{
Identifier = identifier
};
iOSNotificationCenter.RemoveDeliveredNotification(notification.Identifier);
}
public void CreateTimeTrigger(int days, int minutes, int seconds,bool repeats, string title,string subtitle,string body,string identifier=null,string thredidentifier=null,string categoryidentifier=null,string data=null,bool showinforeground=false)
{
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = new TimeSpan(days, minutes, seconds),
Repeats = repeats
};
var notification = new iOSNotification()
{
// You can specify a custom identifier which can be used to manage the notification later.
// If you don't provide one, a unique string will be generated automatically.
Identifier = identifier,
Title = title,
Body = body,
Subtitle = subtitle,
ShowInForeground = showinforeground,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = categoryidentifier,
ThreadIdentifier = thredidentifier,
Data = data,
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
}
public void CreateCalendarTrigger(int year,int month,int day,int hour, int min, int sec, bool repeats, string title, string subtitle, string body, string identifier = null, string thredidentifier = null, string categoryidentifier=null,string data=null, bool showinforeground = false)
{
var calendarTrigger = new iOSNotificationCalendarTrigger()
{
Year = year,
Month = month,
Day = day,
Hour = hour,
Minute = min,
Second = sec,
Repeats = repeats
};
var notification = new iOSNotification()
{
// You can specify a custom identifier which can be used to manage the notification later.
// If you don't provide one, a unique string will be generated automatically.
Identifier = identifier,
Title = title,
Body = body,
Subtitle = subtitle,
ShowInForeground = showinforeground,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = categoryidentifier,
ThreadIdentifier = thredidentifier,
Data = data,
Trigger = calendarTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
}
public void CreateLocationTrigger(float lat, float lng, float radius, bool notifyonentry, bool notifyonexit, string title, string subtitle, string body, string identifier = null, string thredidentifier = null, string categoryidentifier=null,string data=null, bool showinforeground = false)
{
var locationTrigger = new iOSNotificationLocationTrigger()
{
Center = new Vector2(lat, lng),
Radius = radius,
NotifyOnEntry = notifyonentry,
NotifyOnExit = notifyonexit
};
var notification = new iOSNotification()
{
// You can specify a custom identifier which can be used to manage the notification later.
// If you don't provide one, a unique string will be generated automatically.
Identifier = identifier,
Title = title,
Body = body,
Subtitle = subtitle,
ShowInForeground = showinforeground,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = categoryidentifier,
ThreadIdentifier = thredidentifier,
Data = data,
Trigger = locationTrigger
};
iOSNotificationCenter.ScheduleNotification(notification);
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.Notifications.iOS;
using UnityEngine;
public class TestPush : MonoBehaviour
{
private void Awake()
{
PushHelper.PermissionGranted += OnRequestGranted;
PushHelper.PermissionDenied += OnRequestDenied;
goUI_Denied.SetActive(true);
}
private void Start()
{
LocationHelper.LocationFound += ActuallySendPushNotificationLoc;
}
public GameObject goUI_Granted;
public GameObject goUI_Denied;
public PushHelper ph;
void OnRequestGranted()
{
goUI_Granted.SetActive(true);
goUI_Denied.SetActive(false);
}
void OnRequestDenied()
{
goUI_Granted.SetActive(false);
}
public LocationHelper lh;
public void SendPushNotificationNow()
{
ph.CreateTimeTrigger(0, 0, 1, false, "Notify Now", "in 1 second", "Push sent at " + System.DateTime.Now + " "+LocationHelper.latlng.ToString("F4"), "pushnow", "now", "apptest", "testdata", true);
}
public void SendPushNotificationHere()
{
lh.PollLatLng();
}
void ActuallySendPushNotificationLoc(Vector2 latlng)
{
print("Actually Send Push "+latlng);
ph.CreateLocationTrigger(latlng.y, latlng.x, 250, true, true, "Notify Loc", "Arrived", latlng.ToString("F4"),"here","location","location",null,true);
}
public void SendPushEveryMin()
{
ph.CreateTimeTrigger(0, 1, 0, true, "Repeat Every Minute", "Repeating...", "Ta da!","repeatingmin");
}
public void CancelPushEveryMin()
{
ph.CancelNotification("repeatingmin");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment