您的位置:首页 > 其它

极光推送基本写法

2015-09-24 00:14 381 查看
前提:你登录的JPush中上有你需要推送的应用(包名要与你项目的 “Your Package” 一致)

1、导包:

libs\jpush-sdk-release1.6.1.jar

libs\rmeabi\libjpush.so

2、在

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Your Package"
android:versionCode="100"
android:versionName="1.0.0"
>

<!-- Required -->
<permission android:name="com.itheima39.push.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />

<!-- Required -->
<uses-permission android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">.push.permission.JPUSH_MESSAGE" /></span>
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!--since 1.6.0 -->

<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">Application"></span>
<activity
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your  </span><span style="font-family: Arial, Helvetica, sans-serif;">MainActivity"</span>
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">TestActivity"</span>
android:label="@string/app_name">
</activity>
<!-- Required -->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>

<!-- Required -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true" >
<intent-filter android:priority="1000"> <!--since 1.3.5 -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--since 1.3.5 -->
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /> <!--since 1.3.5 --></span>
</intent-filter> <!--since 1.3.5 -->
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">Receiver"</span>
android:enabled="true" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>
</intent-filter>
</receiver>
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>
</intent-filter>
</activity>
<!-- Required SDK核心功能-->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" >
</service>
<!-- Required SDK核心功能-->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />

<!-- Required. For publish channel feature -->
<!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
<!-- 例如: -->
<!-- 发到 Google Play 的APK可以设置为 google-play; -->
<!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
<!-- 目前这个渠道统计功能的报表还未开放。-->
<meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
<!-- Required. AppKey copied from Portal -->
<meta-data android:name="JPUSH_APPKEY" android:value="<span style="font-family: Arial, Helvetica, sans-serif;">Your <span style="color: rgb(50, 63, 72); font-family: ff-tisa-web-pro-1, ff-tisa-web-pro-2, 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'Microsoft YaHei UI', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 14px; line-height: 20px; background-color: rgb(235, 239, 243);">AppKey</span></span><span style="font-family: Arial, Helvetica, sans-serif;">"/> </span>
</application>
</manifest>


3、接受广播

package com.itheima39.push;

import org.json.JSONObject;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}

Bundle bundle = intent.getExtras();
//        Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));

if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Log.d(TAG, "JPush用户注册成功");

} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的自定义消息");

} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的通知");

receivingNotification(context,bundle);

} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");

openNotification(context,bundle);

} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}

private void receivingNotification(Context context, Bundle bundle){
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
Log.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
Log.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.d(TAG, "extras : " + extras);
}

private void openNotification(Context context, Bundle bundle){
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String myValue = "";
try {
JSONObject extrasJson = new JSONObject(extras);
myValue = extrasJson.optString("url");

} catch (Exception e) {
Log.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
if (TYPE_THIS.equals(myValue)) {
Intent mIntent = new Intent(context, ThisActivity.class);
mIntent.putExtra("url", myValue);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
else if (TYPE_ANOTHER.equals(myValue)){
Intent mIntent = new Intent(context, AnotherActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
}

4、Your Application

package Your Package;

import cn.jpush.android.api.JPushInterface;
import android.app.Application;

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
5、Your MainActivity 就是你的布局(随便!根据自己意愿创建布局,可以不用写!)

6、Your TestActivity 你想要根据推送跳转到的界面(如果指定的url地址,如果网络连接可以跳转到指定网址)

package Your Package;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ThisActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thisactivity);
<span style="color:#33cc00;">//得到你指定的字段(设置的什么,它就会跳转到响应的地方!)</span>
String url = getIntent().getStringExtra("url");
Toast.makeText(getApplicationContext(), url, 0).show();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: