1 创建新工程
-----获取api key 和secret key
2,推送设置
3,新建安卓项目,并把应用包名 和 推送中设置的包名设置一致
5,解压,并添加libs文件夹文件到相应路径,并添加pushservice-4.2.0.63.jar到构建路径
6,添加 Demo\src\com\baidu\push\example 路径下的Utils.java到 包下
7,新建MyPushMessageReceiver类并继承FrontiaPushMessageReceiver
8,根据提示初始化所有方法,并复写onNotificationClicked方法
/**
* 接收通知点击的函数。注:推送通知被用户点击前,应用无法通过接口获取通知的内容。
*
* @param context
* 上下文
* @param title
* 推送的通知的标题
* @param description
* 推送的通知的描述
* @param customContentString
* 自定义内容,为空或者json字符串
*/
@Override
public void onNotificationClicked(Context context, String title,
String description, String customContentString) {
String notifyString = "通知点击 title=\"" + title + "\" description=\""
+ description + "\" customContent=" + customContentString;
Log.d(TAG, notifyString); // 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值
if (!TextUtils.isEmpty(customContentString)) {
JSONObject customJson = null;
try {
customJson = new JSONObject(customContentString);
String myvalue = null;
if (customJson.isNull("mykey")) {
myvalue = customJson.getString("mykey");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑
updateContent(context, notifyString);
} private void updateContent(Context context, String content) { MainActivity.url="http://www.sina.com";
MainActivity.num=1;
Intent intent = new Intent();
intent.setClass(context.getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(intent); }
9 修改AndroidManifest.xml文件
----------------------修改application节点 android:name="com.baidu.frontia.FrontiaApplication"
----------------------注册MyPushMessageReceiver类
xml文件如下
<application android:hardwareAccelerated="true"
android:icon="@drawable/icon"
android:label="小洋"
android:name="com.baidu.frontia.FrontiaApplication">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:launchMode="singleTop" android:name=".SplashActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:configChanges="keyboardHidden" android:name=".GuideActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".MainActivity" />
<activity android:configChanges="orientation|keyboardHidden" android:label="MediaViewActivity" android:name="com.baidu.android.pushservice.richmedia.MediaViewActivity" />
<activity android:configChanges="orientation|keyboardHidden" android:label="MediaListActivity" android:launchMode="singleTask" android:name="com.baidu.android.pushservice.richmedia.MediaListActivity" />
<receiver android:name="com.hn.esgj.MyPushMessageReceiver">
- <intent-filter>
-
<action android:name="com.baidu.android.pushservice.action.MESSAGE" />
<action android:name="com.baidu.android.pushservice.action.RECEIVE" />
<action android:name="com.baidu.android.pushservice.action.notification.CLICK" />
</intent-filter>
</receiver>
<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.RegistrationReceiver" 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:exported="true" android:name="com.baidu.android.pushservice.PushService" android:process=":bdservice_v1">
- <intent-filter>
<action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" />
</intent-filter>
</service>
</application>
10 在主active的onCreate方法中 开启
public void onCreate(Bundle savedInstanceState) {
try{
PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, "KH2j92etnCvrO0WyDLUQob4z") ;
}catch (Exception e){ }
super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
super.setBooleanProperty("loadInWebView", true);
super.loadUrl(url);
this.num=0;
this.url="file:///android_asset/www/index.html";
}
11,到百度控制台推送通知做测试
理论上成功,服务器端请下载相应的sdk