android消息推送---JPush(极光推送)

1、Demo结构

2、/JPushDemo/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jpushdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <!-- ++++++++++++++++++++++++++++极光推送配置+++++++++++++++++++++++++++++++++++ -->
    <!-- Required -->
    <permission
        android:name="com.example.jpushdemo.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />
    <!-- Required -->
    <uses-permission android:name="com.example.jpushdemo.permission.JPUSH_MESSAGE" />
    <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" />
    <!-- ++++++++++++++++++++++++++++极光推送配置+++++++++++++++++++++++++++++++++++ -->
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:name="com.example.jpushdemo.app.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.jpushdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </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="com.example.jpushdemo" /> <!-- since 1.3.5 -->
            </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> <!-- Required SDK核心功能 -->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.jpushdemo" />
            </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="98962dcb26cdd1d4739963f5" />
        <!-- 自定义广播 -->
        <receiver
            android:name="com.example.jpushdemo.MyReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required  用户注册SDK的intent -->
                <action android:name="cn.jpush.android.intent.UNREGISTRATION" />
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required  用户接收SDK消息的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required  用户接收SDK通知栏信息的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required  用户打开自定义通知栏的intent -->
                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->
                <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <category android:name="com.example.jpushdemo" />
            </intent-filter>
        </receiver>
       <!--  自定义显示推送通知内容的activity -->
       <activity android:name="com.example.jpushdemo.NotifactionActivity"></activity>
        <!-- ++++++++++++++++++++++++++++极光推送配置+++++++++++++++++++++++++++++++++++ -->
    </application>
</manifest>


3、/JPushDemo/res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>


4、/JPushDemo/res/layout/my_custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3d3d3d"
    android:orientation="horizontal" >
    
    <ImageView 
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:padding="2dp"
        android:id="@+id/img"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:textColor="#ff0000"
            android:padding="2dp"
            android:textSize="18sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text"
            android:padding="2dp"
            android:textColor="#00ff00"
            android:textSize="14sp"/>
        
    </LinearLayout>
   
</LinearLayout>


5、/JPushDemo/res/layout/notifaction_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0FFFF" >
    
    <TextView
        android:id="@+id/title" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/title"
        android:padding="5dp"
        android:textSize="24sp"
        />
    
    
    <TextView
        android:id="@+id/content" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_below="@id/title"        
        android:background="@drawable/title"
        android:padding="5dp"
        android:textSize="24sp"
        />
</RelativeLayout>


6、/JPushDemo/res/drawable/title.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <corners
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />
</shape>


7、/JPushDemo/src/com/example/jpushdemo/app/MyApplication.java

package com.example.jpushdemo.app;
import com.example.jpushdemo.R;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import android.app.Application;
import android.app.Notification;

public class MyApplication extends Application {
 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
  JPushInterface.setDebugMode(true);//设置调试模式
  JPushInterface.init(this);//初始化SDK
  setStyleCustom();
 }
 
 private void setStyleCustom(){
  CustomPushNotificationBuilder builder=new CustomPushNotificationBuilder(this, R.layout.my_custom_layout, R.id.img, R.id.title, R.id.text);
  builder.statusBarDrawable=R.drawable.bb;
  builder.layoutIconDrawable=R.drawable.aa;
  builder.notificationDefaults=Notification.DEFAULT_SOUND;
  builder.notificationFlags=Notification.FLAG_AUTO_CANCEL;
  builder.developerArg0="gly";
  JPushInterface.setPushNotificationBuilder(1, builder);
 }
}


8、/JPushDemo/src/com/example/jpushdemo/MainActivity.java

package com.example.jpushdemo;
import cn.jpush.android.api.JPushInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 @Override
 protected void onResume() {
  super.onResume();
  JPushInterface.onResume(this);
 }
 
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  JPushInterface.onPause(this);
 }
}


9、/JPushDemo/src/com/example/jpushdemo/MyReceiver.java

package com.example.jpushdemo;
import cn.jpush.android.api.JPushInterface;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
public class MyReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle bundle=intent.getExtras();
  System.out.println(intent.getAction()+"++++++++++++++++++++++++++++++++++++++++++=");
  if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){
   Intent mIntent=new Intent(context, NotifactionActivity.class);
   mIntent.putExtras(bundle);
   mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
   context.startActivity(mIntent);
  }
 }
}


10、/JPushDemo/src/com/example/jpushdemo/NotifactionActivity.java

package com.example.jpushdemo;
import cn.jpush.android.api.JPushInterface;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NotifactionActivity extends Activity {
 private TextView titleTextView;
 private TextView contentTextView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.notifaction_layout);
  
  titleTextView=(TextView)findViewById(R.id.title);
  contentTextView=(TextView)findViewById(R.id.content);
  init();
 }
 private void init() {
  Intent intent=getIntent();
  if(intent!=null){
   Bundle bundle=getIntent().getExtras();
   String title=bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
   String content=bundle.getString(JPushInterface.EXTRA_ALERT);
   titleTextView.setText(title);
   contentTextView.setText(content);
  }
  
 }
}


11、运行Demo后,在JPush官网上点击立即发送

 

12、测试结果

点击通知条目

 

好了,推送的消息内容终于到达了我们想让他显示的界面、、、、、、、、、、、、、

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值