精华 分享自己最近写的一个 leancloud推送服务的 cordova插件 :D
发布于 1年前 作者 BenBBear 3176 次浏览 来自 分享

cordova-plugin-leanpush

Cordova plugin for LeanCloud push notification

Installation

  • Fetch from cordova npm
 cordova plugin add cordova-plugin-leanpush  --variable LEAN_APP_ID=<YOUR_LEANCOULD_APP_ID> --variable LEAN_APP_KEY=<YOUR_LEANCOULD_APP_KEY>
  • Add this to your gulpfile.js
gulp.task('lpush-install', function(done){
    require('./plugins/cordova-plugin-leanpush/lpush-installer.js')(__dirname, done);
});
  • npm install --save-dev xml2js && npm install

  • Then exectue this gulp task by running gulp lpush-install in shell.

  • Done.

Known Android Build Issue

See Attention/Android Build Issue

Usage

Init

Put the initialization Code in your “deviceReady” Code Block (like $ionicPlatform.ready)

window.LeanPush.init();

Push Related API

Coresponding to the Leancloud Push documentation.


window.LeanPush.subscribe(channel, success, error)  // 订阅频道
window.LeanPush.unsubscribe(channel, success, error) //退订频道
window.LeanPush.clearSubscription(success, error) //退订所有频道

window.LeanPush.getInstallation(success, error)  //Installation 表示一个允许推送的设备的唯一标示, 对应数据管理平台中的 _Installation 表
// success callback:
// function(data){
//   data = {
//        'deviceType':'android' or 'ios',
//        'installationId': 'android installation id' or 'ios deviceToken'
//        'deviceToken':    'ios deviceToken' or 'android installation id'
//   }
// }


window.LeanPush.onNotificationReceived(callback) // 一个notification到来的回调函数
// callback:
// function(notice){
//  notice = {
//     'prevAppState': 'background' or 'foreground' or 'closed',

//      push到来的时候上一个App状态:
//      android只有 'background' 和 'closed', 因为android所有push都要点击
//      ios都有,因为ios如果app在前台,系统推送的alert不会出现
//      用户没有任何操作,app就自动执行notification的函数不好, 可以加个判断

//     'alert':'Notice Text',
//     'file_url':'Push File',
//     'key':'value'   if you send JSON Type Push, they will map to here.
//   }
// }
    

$rootScope.$on('leancloud:notificationReceived', callback) // 如果你用了angular, 一个notification会在scope上broadcast这个event
// callback:
// function(event, notice){
//    // event is from angular, notice is same above 
// }

Many Thanks to Derek Hsu XD 😁

About Sending Push

Use the JS API: AV.Push that leancloud provide.

LeanAnalytics API

Corresponding code is forked from https://github.com/Hybrid-Force/cordova-plugin-leancloud.

Only a novice for leancloud I am, so

is the better way to go.


Screen Recording

Android

IOS

See the Attention Below, the webview can’t alert when onResume

One

  • notice from close
  • notice while foreground

Two

  • notice from background
mobile

console.log

The debugger in screenshot is GapDebug, debug phonegap in browser :D

Behavior

The onNotificationReceived callback and the $rootScope.$emit('leancloud:notificationReceived') will fires when

IOS

  • app in the foreground, notice comes (won’t show the system notification alert)
  • app in the background, tap the notification to resume it
  • app closed, tap the notification to open it

Android

  • app in the foreground, tap the notification to see it
  • app in the background, tap the notification to resume it
  • app closed, tap the notification to open it

Attention

Android Quirk

In order to receive push from android, I change the default MainActivity and Application Entry in that gulp task. Details in the lpush_installer.js.

So if you use another plugin that also goes this way, then there gonna be conflicts.

Uninstall

For fully uninstallation:

cordova plugin rm cordova-plugin-leanpush
ionic platform rm android && ionic platform rm ios
ionic platform add android && ionic platform add ios

Don’t Use Alert in the IOS inside Notification Callback

alert is a blocking function.

IOS UIWebView

It will cause the app to freeze when you resume the app by clicking notification. (but it seems ok when the app is in the foreground or closed.)

For Android

As far as I try, alert is fine, guess is the difference of webView between IOS and android.

Notification Handler

There are two ways, both will be fired when notification comes

  • onNotificationReceived

  • $rootScope.$emit('leancloud:notificationReceived')

You can choose one of them, but may not both.

Android Build Issue

  • Error: duplicate files during packaging of APK

How to Solve:

insert following code into the android tag of platforms/android/build.gradle

 packagingOptions {
       exclude 'META-INF/LICENSE.txt'
       exclude 'META-INF/NOTICE.txt'
}

It should look like below

android{
   packagingOptions {
       exclude 'META-INF/LICENSE.txt'
       exclude 'META-INF/NOTICE.txt'
   }
    //stuff
}

LICENSE

The MIT License (MIT)

Copyright © 2015 Xinyu Zhang, Derek Hsu


Note:

自己完全新手 >.< mobile 开发方面 只接触过一点点android, 这个插件之前1行objc都没写过 :O

都是靠借鉴师兄的代码: https://github.com/Hybrid-Force/cordova-plugin-leancloud ,另外加上各种google + 推测 😂

分享在这里, 希望能有帮助, 要向大家学习哈 :D

对了github链接在这里: https://github.com/BenBBear/cordova-plugin-leanpush


更新了README

7 回复

我勒个去~~感谢楼主的分享~求邮箱

great job! 有个问题,我用cordova6.0.0,xcode编译时报错Plugins/cordova-plugin-leanpush/CDVLeanPush.m:137:23: error: no visible @interface for ‘UIView’ declares the selector ‘stringByEvaluatingJavaScriptFromString:’ [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];

1 error generated.

不懂object C,求指教

已搞定,提了pull request

回到顶部