概述
1.如果只想看IOS的本地消息推送的话,请参考雨凇的博客:http://www.xuanyusong.com/archives/2632
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.接下来是unity调安卓的程序,记得把安卓的工程弄进unity。以下给出的只是安卓工程打包的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);
}
}
});
}
}
最后
以上就是潇洒薯片为你收集整理的unity android 远程推送,unity安卓和IOS的本地消息推送的全部内容,希望文章能够帮你解决unity android 远程推送,unity安卓和IOS的本地消息推送所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复