unity安卓接入极光推送Demo详解

unity极光推送官方Demo https://github.com/jpush/jpush-unity3d-plugin

 

using JPush;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using Newtonsoft.Json.Linq;//需要添加这部分的引用  
using Newtonsoft.Json;//需要添加这部分的引用  

/// <summary>
/// 参考脚本PluginsDemo
/// </summary>
public class Test : MonoBehaviour {

   // public tex
	// Use this for initialization
	void Start () {
        gameObject.name = "Main Camera";
        JPushBinding.Init(gameObject.name);//初始化并开始推送服务

        JPushBinding.RequestPermission();//必须
        JPushBinding.SetDebug(true);
 JPushBinding.ResumePush();//必须
    }


   
/// <summary>
///  获取java类测试部分
/// </summary>
/// <returns></returns>
    public string GetSomething()
    {
        try
        {
            AndroidJavaClass sss = new AndroidJavaClass("cn.jiguang.unity.push.JPushReceiver");
            sss.Call<string>("GetExtras");
            text.text = sss.ToString();
            return sss.ToString();
        }
        catch (System.Exception e)
        {

            text.text = e.ToString();
            return "没有";
        }
      
    }

    /// <summary>
    /// 调用获取java类测试部分
    /// </summary>
    public void GetIntent()
    {
       GetSomething();
    }

    /// <summary>
    /// 停止 JPush 推送服务。 
    /// </summary>
    public void StopPush()
    {
        JPushBinding.StopPush();
    }

    /// <summary>
    /// 唤醒 JPush 推送服务,使用了 StopPush 必须调用此方法才能恢复。
    /// </summary>
    public  void ResumePush()
    {
        JPushBinding.ResumePush();
    }

    public Text text;

    /// <summary>
    /// 获取当前设备的 Registration Id。
    /// </summary>
    /// <returns>设备的 Registration Id。</returns>
    public  void GetRegistrationId()
    {
        text.text= JPushBinding.GetRegistrationId();

       
    }


    #region 如何设置标签,实现通过标签发送消息  在官网发送推送内容时选择通过标签发送
    int callbackId = 0;
    public void SetTagClick()
    {
        List<string> tags = new List<string>() { "111", "222" };
        JPushBinding.SetTags(callbackId++, tags);

    }

    public void DeleteTagClick()
    {
        List<string> tags = new List<string>();
        tags.Add("addtag1");
        tags.Add("addtag2");

        JPushBinding.DeleteTags(callbackId++, tags);

    }
    public void AddTagClick()
    {
        List<string> tags = new List<string>() { "addtag1", "addtag2" };
        JPushBinding.AddTags(callbackId++, tags);

    }
    #endregion



    //unity极光推送官方Demo https://github.com/jpush/jpush-unity3d-plugin
    //官方注册应用 https://www.jiguang.cn/jpush/#/app/a3f81515bb6d98e9ea43b47e/push/notification
    // 官方文档 https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/
    #region 自动接收消息,自动调用部分
    /* data format
    {
       "message": "hhh",
       "extras": {
           "f": "fff",
           "q": "qqq",
           "a": "aaa"
       }
    }
    */
    // public Text receiveMessageText;
    // 开发者自己处理由 JPush 推送下来的消息。
    void OnReceiveMessage(string jsonStr)
    {
        Debug.Log("recv----message-----" + jsonStr);
        //receiveMessageText.text = "recv----message-----" + jsonStr;
    }



    /**
     * {
     *	 "title": "notiTitle",
     *   "content": "content",
     *   "extras": {
     *		"key1": "value1",
     *       "key2": "value2"
     * 	}
     * }
     */
    public Text receiveNotificationText;
    // 获取的是 json 格式数据,开发者根据自己的需要进行处理。
    //能够得到  推送内容  、通知标题、附加字段
    //推送字段在这里,需要把string格式的jsonStr转换为json,再取其中字段
    void OnReceiveNotification(string jsonStr)
    {
        Debug.Log("recv---notification---" + jsonStr);
        receiveNotificationText.text = "recv---notification---" + jsonStr;
    }


    public Text OnOpenNotificationText;
    public Text titleText;
    public Text contentText;
    public Text extrasText;
    public Text errorText;
    //开发者自己处理点击通知栏中的通知
    //点击通知栏之后开发者自己需要做的事情、、、、、、、、、、有用
    void OnOpenNotification(string jsonStr)
    {
        Debug.Log("recv---openNotification---" + jsonStr);
        OnOpenNotificationText.text = "recv---openNotification---" + jsonStr;

        try
        {
            //需要把string格式的jsonStr转换为json
            var test = JsonConvert.DeserializeObject(jsonStr) as JObject;
            string title = test["title"].ToString();//已得到通知标题
            string content = test["content"].ToString();//已得到推送内容
            string extras = test["extras"].ToString();//已得到附加字段 6666666
            titleText.text = title;
            contentText.text = content;
            extrasText.text = test["extras"]["key"].ToString();          
        }
        catch (Exception e)
        {

            errorText.text=e.ToString();
        }
     
    }


   // public Text OnJPushTagOperateResultText;
    /// <summary>
    /// JPush 的 tag 操作回调。
    /// </summary>
    /// <param name="result">操作结果,为 json 字符串。</param>
    void OnJPushTagOperateResult(string result)
    {
        Debug.Log("JPush tag operate result: " + result);
       // OnJPushTagOperateResultText.text = "JPush tag operate result: " + result;
    }

   // public Text OnJPushAliasOperateResultText;
    /// <summary>
    /// JPush 的 alias 操作回调。
    /// </summary>
    /// <param name="result">操作结果,为 json 字符串。</param>
    void OnJPushAliasOperateResult(string result)
    {
        Debug.Log("JPush alias operate result: " + result);
      //  OnJPushAliasOperateResultText.text = "JPush alias operate result: " + result;
    }

   // public Text OnGetRegistrationIdText;
    void OnGetRegistrationId(string result)
    {
        Debug.Log("JPush on get registration Id: " + result);
       // OnGetRegistrationIdText.text = "JPush on get registration Id: " + result;
    }
    #endregion



}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值