欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

phonegap 推送–百度云推送整合phonegap

phonegap开发 夜阑小雨 4608℃ 0评论

 

最近百度上线了百度推送云。基于研究的目的,我做了研究,发现百度云还是比较牛的。可以推送通知 ,推送消息,还能推送富媒体,同时还是免费的。

对于消息推送到客户端后,打开推送通知后的动作也做了个性化定制,可以打开app 和 网址,通知支持自定义动作。

作为phonegap的爱好者,我试着将百度推送整合到phonegap的项目中。

 

1.先去百度开发者中心新建项目填写相关的信息。

03.png

2.下载sdk包 或者下载网址配置的demo包。

 

3.开始整合:

拷贝相应的文件到项目中。

主要是这个文件pushservice-2.1.0.jar

4,复制sdk包中的 drawable 和 layout 文件到项目res中。

5.修改AndroidManifest.xml文件。

 

添加

<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
<uses-permission android:name=”android.permission.BROADCAST_STICKY” />
<uses-permission android:name=”android.permission.WRITE_SETTINGS” />
<uses-permission android:name=”android.permission.VIBRATE” />
<!– for log. –>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
<uses-permission android:name=”android.permission.ACCESS_DOWNLOAD_MANAGER”/>
<uses-permission android:name=”android.permission.DOWNLOAD_WITHOUT_NOTIFICATION” />
<uses-permission android:name=”android.permission.SYSTEM_ALERT_WINDOW”/>
<uses-permission android:name=”android.permission.DISABLE_KEYGUARD” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

 

添加

 

<activity
android:name=”你的app包名.CustomActivity”
android:configChanges=”orientation|keyboardHidden”
android:label=”Push_notification_test” >
</activity>

<!– push service rich media display –>
<activity
android:name=”com.baidu.android.pushservice.richmedia.MediaViewActivity”
android:configChanges=”orientation|keyboardHidden”
android:label=”MediaViewActivity” >
</activity>
<activity
android:name=”com.baidu.android.pushservice.richmedia.MediaListActivity”
android:configChanges=”orientation|keyboardHidden”
android:label=”MediaListActivity”
android:launchMode=”singleTask” >
</activity>
<receiver android:name=”你的app包名.PushMessageReceiver”>
<intent-filter>
<!– 接收push消息 –>
<action android:name=”com.baidu.android.pushservice.action.MESSAGE” />
<!– 接收bind,unbind,fetch,delete等反馈消息 –>
<action android:name=”com.baidu.android.pushservice.action.RECEIVE” />
<action android:name=”com.baidu.android.pushservice.action.notification.CLICK” />
</intent-filter>
</receiver>
<!– push service –>
<receiver android:name=”com.baidu.android.pushservice.PushServiceReceiver”
android:process=”:bdservice_v1″>
<intent-filter>
<action android:name=”android.intent.action.BOOT_COMPLETED” />
<action android:name=”android.net.conn.CONNECTIVITY_CHANGE” />
<action android:name=”com.baidu.android.pushservice.action.notification.SHOW” />
<action android:name=”com.baidu.android.pushservice.action.media.CLICK” />
</intent-filter>
</receiver>

<receiver android:name=”com.baidu.android.pushservice.RegistratonReceiver”
android:process=”:bdservice_v1″>
<intent-filter>
<action android:name=”com.baidu.android.pushservice.action.METHOD” />
<action android:name=”com.baidu.android.pushservice.action.BIND_SYNC” />
</intent-filter>
<intent-filter>
<action android:name=”android.intent.action.PACKAGE_REMOVED”/>
<data android:scheme=”package” />
</intent-filter>
</receiver>
<service
android:name=”com.baidu.android.pushservice.PushService”
android:exported=”true”
android:process=”:bdservice_v1″ />
<meta-data android:name=”api_key” android:value=”您的appkey />

 

6. 修改主文件:

添加

 

import com.baidu.android.common.logging.Log;
import com.baidu.android.pushservice.richmedia.MediaListActivity;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.breadth.pgtest.PGtestActivity;
import com.breadth.pgtest.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.breadth.pgtest.R;

 

在主Activity中添加:

 

RelativeLayout mainLayout = null;
TextView text = null;
Button initButton = null;
Button setButton = null;
Button tokenButton = null;
Button initAKButton = null;
Button richButton = null;
Button setTagsButton = null;
Button delTagsButton = null;
public static int initialCnt = 0;
private boolean isLogin = false;

private static final String TAG = “PushDemoActivity”;
public static final String RESPONSE_METHOD = “method”;
public static final String RESPONSE_CONTENT = “content”;
public static final String RESPONSE_ERRCODE = “errcode”;
protected static final String ACTION_LOGIN = “com.breadth.pgtest.action.LOGIN”;
public static final String ACTION_MESSAGE = “com.breadth.pgtest.action.MESSAGE”;
public static final String ACTION_RESPONSE = “bccsclient.action.RESPONSE”;
public static final String ACTION_SHOW_MESSAGE = “bccsclient.action.SHOW_MESSAGE”;
protected static final String EXTRA_ACCESS_TOKEN = “access_token”;
public static final String EXTRA_MESSAGE = “message”;

 

在public void onCreate(Bundle savedInstanceState) 中添加

 

PushManager.startWork(getApplicationContext(),
PushConstants.LOGIN_TYPE_API_KEY, Utils.getMetaValue(PGtestActivity.this, “您的appkey”));

 

在主activity后面添加以下内容

 

public void onStart() {
super.onStart();
Log.d(TAG, “>=====onStart1=====<“);
Log.d(TAG, “>=====Intent2=====<“+this.getIntent());

Intent recIntent = this.getIntent();
String openType = “”
+ recIntent.getIntExtra(PushConstants.EXTRA_OPENTYPE, 0);
String msgId = recIntent.getStringExtra(PushConstants.EXTRA_MSGID);

Log.d(TAG,
“Collect Activity start feedback info , package2:”
+ this.getPackageName() + ” openType: ” + openType
+ ” msgid: ” + msgId);

PushManager.activityStarted(this);
}

@Override
public void onResume() {
super.onResume();
Log.d(TAG, “>=====onResume3=====<“);
showChannelIds();
}

@Override
protected void onNewIntent(Intent intent) {
// 如果要统计Push引起的用户使用应用情况,请实现本方法,且加上这一个语句
//setIntent(intent);
Log.d(TAG, “>=====onNewIntent4=====<“);
handleIntent(intent);
}

@Override
public void onStop() {
super.onStop();
Log.d(TAG, “>=====onStop1=====<“);
PushManager.activityStoped(this);
}

/**
* 处理Intent
*
* @param intent
*            intent
*/
private void handleIntent(Intent intent) {
String action = intent.getAction();
Log.d(TAG, “Handle intent: rn” + intent);

if (ACTION_RESPONSE.equals(action)) {

String method = intent.getStringExtra(RESPONSE_METHOD);

if (PushConstants.METHOD_BIND.equals(method)) {
Log.d(TAG, “Handle bind response”);
String toastStr = “”;
int errorCode = intent.getIntExtra(RESPONSE_ERRCODE, 0);
if (errorCode == 0) {
String content = intent.getStringExtra(RESPONSE_CONTENT);
String appid = “”;
String channelid = “”;
String userid = “”;

try {
JSONObject jsonContent = new JSONObject(content);
JSONObject params = jsonContent
.getJSONObject(“response_params”);
appid = params.getString(“appid”);
channelid = params.getString(“channel_id”);
userid = params.getString(“user_id”);
} catch (JSONException e) {
Log.e(TAG, “Parse bind json infos error: ” + e);
}

SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sp.edit();
editor.putString(“appid”, appid);
editor.putString(“channel_id”, channelid);
editor.putString(“user_id”, userid);
editor.commit();

showChannelIds();

toastStr = “Bind Success”;
} else {
toastStr = “Bind Fail, Error Code: ” + errorCode;
if (errorCode == 30607) {
Log.d(“Bind Fail”, “update channel token—–!”);
}
}

Toast.makeText(this, toastStr, Toast.LENGTH_LONG).show();
}
} else if (ACTION_LOGIN.equals(action)) {
String accessToken = intent.getStringExtra(EXTRA_ACCESS_TOKEN);
PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_ACCESS_TOKEN, accessToken);
isLogin = true;
//initButton.setText(R.string.text_btn_init1);
} else if (ACTION_MESSAGE.equals(action)) {
String message = intent.getStringExtra(EXTRA_MESSAGE);
String summary = “Receive message from server:nt”;
JSONObject contentJson = null;
String contentStr = message;
try {
contentJson = new JSONObject(message);
contentStr = contentJson.toString(4);
} catch (JSONException e) {
Log.d(TAG, “Parse message json exception.”);
}
summary += contentStr;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(summary);
builder.setCancelable(true);
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
} else {
Log.i(TAG, “Activity normally start!”);
}
}

private void showChannelIds() {
String appId = null;
String channelId = null;
String clientId = null;
Log.d(TAG, “>=====showChannelIds5=====<“);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
appId = sp.getString(“appid”, “”);
channelId = sp.getString(“channel_id”, “”);
clientId = sp.getString(“user_id”, “”);
//text = (TextView) findViewById(R.id.text);

String content = “tApp ID: ” + appId + “ntChannel ID: ” + channelId
+ “ntUser ID: ” + clientId + “nt”;
Log.d(TAG, “Content: ” + content);
//if (text != null) {
//text.setText(content);
//}
//text.invalidate();
Log.d(TAG, “>=====showChannelIds6=====<“);
}

这里主程序就修改完了

7.在添加相其他Activity文件

这里直接把我的附件的文件拷贝进去然后修改相应的Activity名称。不会回报错。

 

CustomActivity.java 自定义接受容器用来展示消息和通知

PushMessageReceiver.java 用来接受通知消息

Utils.java 用来对信息进行编码解码的文件

 

8.这时候就可以跑app。

跑起来之后,通过云推送的服务器端,就可以发送通知和消息。富媒体也可以。

 

当然这样的还是有些问题,不过基本流程完了。可以自己再做修改。

 

附件下载:lib

 

 

 

 

转载请注明:夜阑小雨 » phonegap 推送–百度云推送整合phonegap

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址