• Unity安卓本地推送


    https://blog.csdn.net/macaopark/article/details/69053691

    新建推送脚本:LocalReceive

    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.TaskStackBuilder;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.Resources;
    
    import com.unity3d.player.UnityPlayer;
    
    
    public class LocalReceiver extends BroadcastReceiver
    {
    
    //    private static Set<String> channels = new HashSet();
        private NotificationManager manager;
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    //        Toast.makeText(context, "开始工作了!!!", Toast.LENGTH_LONG).show();
            //顶部通知
            manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            //点击通知栏跳转
            Intent playIntent = new Intent(context, UnityPlayerActivity.class);
            //PendingIntent,和Intent类似,不同的是由于不是马上调用
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Notification.Builder builder = new Notification.Builder(context);
            Resources res = context.getResources();
            builder.setContentTitle("title").setContentText("提醒内容").setSmallIcon(R.drawable.ic_launcher).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true).setSubText("早点休息!");
            int app_icon = res.getIdentifier("app_icon", "drawable", context.getPackageName());
            builder.setSmallIcon(app_icon);
    
            final Notification notify = builder.build();
    
            manager.notify(1, builder.build());
        }
    
        public static void ClearShowingNotifications() {
            Activity currentActivity = UnityPlayer.currentActivity;
            @SuppressLint("WrongConstant") NotificationManager nm = (NotificationManager)currentActivity.getSystemService("notification");
            nm.cancelAll();
        }
    
    }
    

     UnityPlayerActivity中调用:

     //先清空 本地推送内容
        public  void ClearShowing()
        {
            LocalReceiver.ClearShowingNotifications();
        }
    
        //android 推送功能
        public  void AndroidNotification()
        {
            //LocalReceiver.SetRepeatingNotification(id,delayMs,title,message,ticker,rep,sound,soundName,vibrate,lights,largeIconResource,smallIconResource,bgColor,bundle,channel,actions);
            setReminder(true);
        }
    
        public void setReminder(boolean reminder)
        {
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            PendingIntent pi = PendingIntent.getBroadcast(UnityPlayerActivity.this, 0, new Intent(this, LocalReceiver.class), 0);
            if (reminder) {
                //设置闹钟
                Calendar instance = Calendar.getInstance();
                instance.setTimeInMillis(System.currentTimeMillis());
                instance.setTimeZone(TimeZone.getTimeZone("GET+8"));
                instance.add(Calendar.SECOND, 5);//设置5秒
                am.setRepeating(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), 60000, pi);
    //          am.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(),pi);
            }else {
                am.cancel(pi);//关闭闹钟
    
            }
        }
    

      回到Unity中:

      void Awake ()
      {
              ClearNotifications();//android  清空推送
      }
      void Start ()
      {
              androidNotification();//android 推送
      }
    //android  推送
        public void androidNotification()
        {
            //LocalNotification.SendRepeatingNotification(1, 5000, 5000, "一起捕鱼吧", "炮声一响,黄金万两", new Color32(0xff, 0x44, 0x44, 255));
            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            jo.Call("AndroidNotification");
            Debug.Log("android   通知功能=======");
        }
    
        //android  清空推送
        public void ClearNotifications()
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            jo.Call("ClearShowing");
            Debug.Log("android   清空通知功能======="); 
        }
    

      

      

  • 相关阅读:
    CoreData学习-最好的一片文章
    Xcode4.6下添加百度地图ios版(BMapKit)详细教程(_BMKMapManager错误解决)
    NSSearchPathForDirectoriesInDomains用法
    编绎显示Unknown type name “CGFloat” 错误解决方法
    自动调整cell的高度
    sqlMapConfig约束
    PotPlayer左上角信息关闭
    松懈
    sql查询练习
    idea内容补充
  • 原文地址:https://www.cnblogs.com/jbw752746541/p/9609590.html
Copyright © 2020-2023  润新知