推送可以及时,主动的与用户发起交互

(1)继承jar包,照示例AndroidManifest.xml添加.

(2)自定义MyApp继承自Application,在onCreate方法中调用JPushInterface.init(MainActivity.this);

或者在Activity的onCreate中调用.

(3)另外,在activity的onResume方法要调用JPushInterface.onResume(this);否则,推送不会出现,

在onPause中调用JPushInterface.onPause(this);

这样,可以通过服务器往安装了App的所有用户发送一条推送.

 

通过Alias往客户端发送信息.

在客户端的onCreate中

JPushInterface.setAlias(MainActivity.this, "aa", new TagAliasCallback() {                    @Override                    public void gotResult(int arg0, String arg1, Set arg2) {                        Log.e("info",arg1+"-----------");                        //arg1是tag                    }                });

别名和签名设置的异常处理

有时会因为网络原因,有一定几率设置别名或标签失败.

privatevoidsetAlias() {EditText aliasEdit = (EditText) findViewById(R.id.et_alias);String alias = aliasEdit.getText().toString().trim();if(TextUtils.isEmpty(alias)) {Toast.makeText(PushSetActivity.this,R.string.error_alias_empty, Toast.LENGTH_SHORT).show();return;}if(!ExampleUtil.isValidTagAndAlias(alias)) {Toast.makeText(PushSetActivity.this,R.string.error_tag_gs_empty, Toast.LENGTH_SHORT).show();return;} // 调用 Handler 来异步设置别名mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));}privatefinalTagAliasCallback mAliasCallback =newTagAliasCallback() {@OverridepublicvoidgotResult(intcode, String alias, Set tags) {String logs ;switch(code) {case:logs ="Set tag and alias success";Log.i(TAG, logs);break;case:logs ="Failed to set alias and tags due to timeout. Try again after 60s.";Log.i(TAG, logs);mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias),*);break;default:logs ="Failed with errorCode = "+ code;Log.e(TAG, logs);} ExampleUtil.showToast(logs, getApplicationContext());}};privatestaticfinalintMSG_SET_ALIAS =;privatefinalHandler mHandler =newHandler() {@OverridepublicvoidhandleMessage(android.os.Message msg) {super.handleMessage(msg);switch(msg.what) {caseMSG_SET_ALIAS:Log.d(TAG,"Set alias in handler.");// 调用 JPush 接口来设置别名。JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj,null, mAliasCallback);break;default:Log.i(TAG,"Unhandled msg - "+ msg.what);}}};

自定义通知栏的样式

自定义样式放在init()之后.

CustomPushNotificationBuilder builder=new CustomPushNotificationBuilder(MainActivity.this, R.layout.my_push, R.id.iv_push, R.id.tv_title, R.id.tv_content);                builder.statusBarDrawable=R.drawable.ic_category_2;//最顶层状态栏小图标                builder.layoutIconDrawable=R.drawable.ic_category_2;  //下拉状态时显示的通知图标.                JPushInterface.setPushNotificationBuilder(2, builder);JPushInterface.setDefaultPushNotificationBuilder(builder); //设置该对话框为默认.

自定义消息:

所接收的消息不再局限于Notification,而是可以直接取出消息中的内容,从而用自己的方式显示给用户.

此时需要自定义一个MyReceiver继承自BroadcastReceiver.

public class MyReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context ctx, Intent intent) {        Bundle bundle =intent.getExtras();   //接受到消息                Log.e("info", "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));          if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);                Log.d("info", "[MyReceiver] 接收Registration Id : " + regId);                //send the Registration Id to your server...                                        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {                Log.d("info", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));//                processCustomMessage(ctx, bundle);                        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {                Log.d("info", "[MyReceiver] 接收到推送下来的通知");                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);                Log.d("info", "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);                            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {                Log.d("info", "[MyReceiver] 用户点击打开了通知");                JPushInterface.reportNotificationOpened(ctx, bundle.getString(JPushInterface.EXTRA_MSG_ID));               //                //打开自定义的Activity                Intent i = new Intent(ctx, TwoActivity.class);                i.putExtras(bundle);                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                ctx.startActivity(i);                            }            }    // 打印所有的 intent extra 数据    private static String printBundle(Bundle bundle) {        StringBuilder sb = new StringBuilder();        for (String key : bundle.keySet()) {            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {                sb.append("/nkey:" + key + ", value:" + bundle.getInt(key));            } else {                sb.append("/nkey:" + key + ", value:" + bundle.getString(key));            }        }        return sb.toString();    }

在类中接收完消息后,还需要在 AndroidManifest.xml中添加

                                                

获取 RegistrationID API

集成了JPush SDK的应用程序第一次注册到JPush服务器时,服务器会返回一个唯一的该设备的标识:RegistertionID.

String id=JPushInterface.getRegistrationID(MainActivity.this);

 

调用网络接口来发送消息

 

 

sendno:发送的编号.

app_key: 应用程序的appKey

receiver_type:接受者的类型 ----2.指定tag----3.指定alias----4.广播----5.根据registrationId进行推送.

msg_content:发送的内容,在这里必须要JSON格式.

platform:要发送的平台

verfication_code:将sendno+receiver_typ+receiver_values+API MasterSecret(在应用的详细信息里面)字符串拼接起来后,用md5加密

 

设置保留最近通知条数 API

JPushInterface.init(context);

JPushInterface.setLatestNotificationNumber(context,);保留最近的3条




更多相关文章

  1. Android 通知栏Notification的整合
  2. 2010-03-06 传智播客—Android(六)通知、样式、主题、HTML
  3. android 消息机制与仿新闻客户端
  4. Android之Handler消息机制
  5. Android基础-------Android通知Notification
  6. Android仿虾米音乐播放器之通知栏notification解析
  7. Android消息循环分析
  8. Android异步消息处理之Thread+Handler
  9. android的消息处理机制(Looper,Handler,Message)

随机推荐

  1. 判断移动终端是安卓还是iOS
  2. Android Studio(十一):代码混淆及打包apk
  3. Android(安卓)存储:Internal Storage的用
  4. 自己编写Android手电筒,支持5.0
  5. android通知栏消息
  6. Android开发:Android快速开发不可或缺的11
  7. Android保存图片到系统相册
  8. Android(安卓)调试桥(adb)
  9. 复习TextView(查漏补缺)
  10. 移植rtmpdump(librtmp)到android