유 니 티 안 드 로 이 드 와 IOS 의 로 컬 메시지 푸 시
8635 단어 unity
2. 다음은 안 드 로 이 드 와 IOS 메 시 지 를 푸 시 하 는 간단 한 인 스 턴 스 입 니 다. 코드:
using UnityEngine;
using System.Collections;
using System;
public class NewBehaviourScript : MonoBehaviour {
#if !UNITY_EDITOR && UNITY_ANDROID
private AndroidJavaClass localPushService;
#endif
//
public void NotificationMessage(string message, int hour, bool isRepeatDay)
{
#if !UNITY_EDITOR && UNITY_IPHONE
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);
#elif !UNITY_EDITOR && UNITY_ANDROID
//@Remark Android 40 ,ID 40
//if(id >= 40) return;
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
//int fixHour = Mathf.Clamp(hour, 0, 23);
//int fixMinute = Mathf.Clamp(Minute, 0, 59);
//System.DateTime newDate = new System.DateTime(year, month, day, fixHour, fixMinute, seconds);
System.DateTime newDate = new System.DateTime(year, month, day, hour, 0, 0);
NotificationMessage(message, newDate, isRepeatDay);
#endif
}
//
public void NotificationMessage(string message, System.DateTime newDate, bool isRepeatDay)
{
#if !UNITY_EDITOR && UNITY_IPHONE
//
if (newDate > System.DateTime.Now)
{
LocalNotification localNotification = new LocalNotification();
localNotification.fireDate = newDate;
localNotification.alertBody = message;
localNotification.applicationIconBadgeNumber = 1;
localNotification.hasAction = true;
if (isRepeatDay)
{
//
localNotification.repeatCalendar = CalendarIdentifier.ChineseCalendar;
localNotification.repeatInterval = CalendarUnit.Day;
}
localNotification.soundName = LocalNotification.defaultSoundName;
NotificationServices.ScheduleLocalNotification(localNotification);
}
#elif !UNITY_EDITOR && UNITY_ANDROID
//@Remark Android 40 ,ID 40
//if(id >= 40) return;
if(localPushService != null)
localPushService.CallStatic("ScheduleNotification", 1, message, GetTimeInMillis(newDate));
#endif
}
void Awake()
{
#if !UNITY_EDITOR && UNITY_ANDROID
localPushService = new AndroidJavaClass("com.simple.dailynotify.DailyNotifyService");//com.simple.dailynotify.DailyNotifyService com.netease.pushSdk.DailyNotifyService
#endif
// , ,
CleanNotification();
}
void OnApplicationPause(bool paused)
{
//
if (paused)
{
Debug.Log("hehe");
//10
NotificationMessage("wwb : 10 ", System.DateTime.Now.AddSeconds(10), false);
// 12
//NotificationMessage("wwb : 12 ", 12, true);
}
else
{
//
CleanNotification();
}
}
//
void CleanNotification()
{
#if !UNITY_EDITOR && UNITY_IPHONE
LocalNotification l = new LocalNotification();
l.applicationIconBadgeNumber = -1;
NotificationServices.PresentLocalNotificationNow(l);
NotificationServices.CancelAllLocalNotifications();
NotificationServices.ClearLocalNotifications();
#elif !UNITY_EDITOR && UNITY_ANDROID
if(localPushService != null)
localPushService.CallStatic("CancelAllNotifications");
#endif
}
//
public long GetTimeInMillis(DateTime date)// TODO:
{
//DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);
//return Convert.ToInt64((date - epoch).TotalSeconds) * 1000;
//DateTime epoch = new DateTime(1970, 1, 1, 8, 0, 0);
//return Convert.ToInt64((date - epoch).TotalSeconds) * 1000;
// 8
int GameTimeZone = 8;
DateTime epoch = new DateTime(1970, 1, 1, 8, 0, 0);
return Convert.ToInt64((date - epoch).TotalSeconds) * 1000 + (GameTimeZone - 8) * 3600 * 1000;
}
}
3. 다음은 유 니 티 가 안 드 로 이 드 를 조정 하 는 프로그램 입 니 다. 안 드 로 이 드 의 공 사 를 유 니 티 에 넣 는 것 을 기억 하 세 요.다음은 안 드 로 이 드 프로젝트 에 포 장 된 jar 파일 입 니 다.
package com.simple.dailynotify;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import java.util.Calendar;
public class DailyNotifyService
{
public static void ScheduleNotification(final int id, final String message, final long time)
{
Activity activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable()
{
public void run()
{
Intent resultIntent = new Intent("com.simple.dailynotifyintent");
resultIntent.setClass(DailyNotifyService.this, BYDailyNotifyReceiver.class);
resultIntent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
resultIntent.putExtra("ID", id);
resultIntent.putExtra("Title", DailyNotifyService.GetApplicationName());
resultIntent.putExtra("Content", message);
resultIntent.putExtra("packageName", DailyNotifyService.this.getPackageName());
AlarmManager am = (AlarmManager)DailyNotifyService.this.getSystemService("alarm");
PendingIntent sender = PendingIntent.getBroadcast(DailyNotifyService.this, id, resultIntent, 134217728);
am.setRepeating(0, time, 86400000L, sender);
Calendar cal = Calendar.getInstance();
cal.add(13, 10);
Log.d("AndroidNative", "Time " + time + " " + cal.getTimeInMillis());
}
});
}
public static String GetApplicationName()
{
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try
{
Activity activity = UnityPlayer.currentActivity;
packageManager = activity.getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(activity.getPackageName(), 0);
}
catch (PackageManager.NameNotFoundException e)
{
applicationInfo = null;
}
String applicationName = (String)packageManager.getApplicationLabel(applicationInfo);
Log.d("AndroidNative", "applicationName " + applicationName);
return applicationName;
}
public static void CancelNotification(final int id)
{
Activity activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable()
{
public void run()
{
NotificationManager nm = (NotificationManager)DailyNotifyService.this.getSystemService("notification");
nm.cancel(id);
Intent resultIntent = new Intent("com.simple.dailynotifyintent");
resultIntent.setClass(DailyNotifyService.this, BYDailyNotifyReceiver.class);
resultIntent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
AlarmManager am = (AlarmManager)DailyNotifyService.this.getSystemService("alarm");
PendingIntent sender = PendingIntent.getBroadcast(DailyNotifyService.this, id, resultIntent, 268435456);
am.cancel(sender);
Log.d("AndroidNative", "LocalNotificationsController canselNotification with id:" + id);
}
});
}
public static void CancelAllNotifications()
{
Activity activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable()
{
public void run()
{
NotificationManager nm = (NotificationManager)DailyNotifyService.this.getSystemService("notification");
nm.cancelAll();
AlarmManager am = (AlarmManager)DailyNotifyService.this.getSystemService("alarm");
Intent resultIntent = new Intent("com.simple.dailynotifyintent");
resultIntent.setClass(DailyNotifyService.this, BYDailyNotifyReceiver.class);
resultIntent.setData(Uri.parse("content://calendar/calendar_alerts/1"));
for (int i = 0; i < 40; i++)
{
PendingIntent sender = PendingIntent.getBroadcast(DailyNotifyService.this, i, resultIntent, 0);
am.cancel(sender);
}
}
});
}
}