Flutter接入極光推送

(1)搜索 https://pub.dartlang.org/packages/jpush_flutter ,安裝插件,而且按照官方配置 /android/app/build.gradleandroid

android: {
  ....
  defaultConfig {
    applicationId "替換成本身應用 ID"
    ...
    ndk {
	//選擇要添加的對應 cpu 類型的 .so 庫。
	abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64' // 'arm64-v8a',        
    }

    manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,
        JPUSH_APPKEY : "appkey", // NOTE: JPush 上註冊的包名對應的 Appkey.
        JPUSH_CHANNEL : "developer-default", //暫時填寫默認值便可.
    ]
  }    
}

(2)Flutter 新建頁面,我放在Welcome頁面ios

class SplashPageState extends State<SplashPage> implements OnSkipClickListener {
  JPush jPush = new JPush();
  String registerId;
  _startupJpush() {
    jPush.setup(appKey: "c52495cbcbc37ee42f04e751", channel: "developer-default",debug: true);
  }
 
 
  _getRegisterID() async {
    registerId = await jPush.getRegistrationID();
    print('*********registerid=' + registerId);
    return registerId;
  }
 
 
  _setPushTag() {
    List<String> tags = List<String>();
    tags.add("jason");
    jPush.setTags(tags);
  }
 
 
  _addEventHandler() {
    // Future<dynamic>event;
    jPush.addEventHandler(onReceiveNotification: (Map<String, dynamic> event) {
      print('*********addOnreceive>>>>>>$event');//進程運行時候能夠接受
      var title = event['alert'];
      var extra = json.decode(event['extras']['cn.jpush.android.EXTRA']);
      notifyRoute(extra['type'],title,extra['id']);
      print('*********msg:$event');
    }, onOpenNotification: (Map<String, dynamic> event) {
      print('*********addOpenNoti>>>>>$event'); //進程關閉的時候能夠接受
      var title = event['alert'];
      var extra = json.decode(event['extras']['cn.jpush.android.EXTRA']);
      notifyRoute(extra['type'],title,extra['id']);
    }, onReceiveMessage: (Map<String, dynamic> event) {
      print('*********addReceiveMsg>>>>>$event'); //進程運行時候能夠接受
      print(event.toString());
      var jsStr = json.decode(event.toString());
    });
}
 
 
//推送跳轉
void notifyRoute(String type,String title,String id) {
  if(type!=null) {
    switch(type){
    case 'news':
      Navigator.of(context).push(new MaterialPageRoute(builder: (ctx) => new WebView(title: title, url:id )));
      break;
    case 'tmdetail':
      Navigator.of(context).push(new MaterialPageRoute(builder: (ctx) => new DetailPage(tmId: int.parse(id))));
      break;
  }
  }
}
 
 
@override
void initState() {
// TODO: implement initState
  super.initState();
  _startupJpush();
  _setPushTag();
  _addEventHandler();
  _getRegisterID();
  _getWelcomeImage();
}
}

(3) 個人用是Extras 而且在客戶端 經過參數 進行指定頁面跳轉json

(4)服務端推送,C# Core版本 nuget搜索JiGuang.JPushapp

/// <summary>
/// 極光推送全局推送
/// </summary>
/// <param name="title">標題</param>
/// <param name="content">正文</param>
/// <param name="dic">extras字典</param>
/// <param name="indent">指定安卓頁面,沒用</param>
/// <param name="outmsg"></param>
/// <returns></returns>
public static bool Send(string title,string content, Dictionary<string,object> dic, Dictionary<string, object> indent,out string outmsg)
{
  var client = new Jiguang.JPush.JPushClient(appKey,appSecret);
  var android = new Android { Title = title, Alert = content, AlertType = 0, Extras = dic, Indent = indent };
  var ios = new IOS{ Alert= title, Extras= dic };
  var pushInfo = HD.DLL.Push.GetTopInfo();
  if(pushInfo!=null&&pushInfo.Id>0 )
  {
  var ts = System.DateTime.Now.Subtract(pushInfo.Addtime);
  if(ts.Hours<1)
  {
    outmsg = "最近的全局推送時間:"+pushInfo.Addtime+",請間隔一個小時";
    return false;
  }
  }
  var playLoad = new Jiguang.JPush.Model.PushPayload() {
    Platform = "all",
    //Audience = "all",
    Notification = new Notification() { Alert=title, Android =android,IOS=ios},
    Message = new Jiguang.JPush.Model.Message() { Content=content,Title=title,Extras= dic }
  };
  var response=client.SendPush(playLoad);
  outmsg = response.Content;
  var ret= response.StatusCode== System.Net.HttpStatusCode.OK;async

  //記錄推送日誌
  pushInfo.Addtime = System.DateTime.Now;
  pushInfo.Title = title;
  pushInfo.Content = content;
  pushInfo.UserId = 0;
  pushInfo.Extras = JsonConvert.SerializeObject(dic);
  pushInfo.Status = ret ? 1 : -1;
  Push.Add(pushInfo);
  return ret;
}ide

相關文章
相關標籤/搜索