Jpush使用总结

今天对极光推送(Jpush )的使用方法做了个测试,首先在其官方网站上下载Android的jar包和demo,(也可以在注册并且输入包名的情况下下载demo,此时的demo就是一个已经配置好包名和APIKey的demo,可以直接将工程部署到手机上进行测试。)。

极光使用了两种不同的通知方式,一种是推送通知,一种是推送消息。
    1.推送通知,此时发送的内容只是一条简单的文本消息。

点击消息后,会跳转到一个Activity(此Activity需要在清单文件中注册过滤器  
  <activity android:name = "com.example.jpushdemo.TestActivity" >
            <intent-filter>
                <action android:name="jpush.testAction" />
                <category android:name="jpush.testCategory" />
            </intent-filter>
         </activity>
  )中,可以在此Activity中以获取传递Intent的方式获取到message的对象。并将其显示在Activity中。
    tv_title = (TextView) findViewById(R.id.tv_title);
        tv_content = (TextView) findViewById(R.id.content);
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            tv_title.setText(title);
            tv_content.setText(content);
        }
一般来说如果不需要太多复杂内容的话,可以直接使用此种方式,将数据封装成一个Json,将其推送,客户端收到后将json解析出来并展现到前台即可。
此种方式有缺陷 ,极光中对推送通知中文本的大小有限制,为72个汉字(或144个其它类型字符)。传输信息有限。
 
    2.推送消息,根据Demo可以看到,自定义消息接收需要客户端接收一个消息广播接收者。
 
   <!-- User defined.  For test only  用户自定义的广播接收器-->
        <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-->
                <category android:name="org.linwoain.test" />
            </intent-filter>
        </receiver>
 
在MyReceiver中有提示
/**
 * 自定义接收器
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver
 
消息只能在此Receiver中获取,不会显示成通知,或者其他。在 onReceive(Context context, Intent intent)中获取方式为
 
 if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                  Bundle bundle = intent.getExtras();  
            Log.d(TAG"[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
}
在此处也可获取推送通知中的内容
 else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG"[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG"[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
  
                Log. i ( TAG , bundle.getString(JPushInterface. EXTRA_ALERT ));//此处获取到的文本就是通知的内容
 
此种方式明显比推送通知的方式好一点的是其发送的信息容量比较大,为 236个汉字。
 
除了在网页中控制台可以推送消息外,也可以在第三方平台上发出推送。
 
是源代码公开的。可以直接下载jar包  https://github.com/jpush/jpush-api-java-client/releases
此jar包运行依赖于另外两个包
  • gson
  • slf4j
将此三个包加入到新建的java工程中。同时在main函数写入:
    //这是在极光网站上申请的密钥
        String masterSecret="*****7a513c1406ee45658f";
        //应用的appKey,同样在网站上申请
        String appKey="*****50726fb7d65b4c07eda";
        //建立JpushClient类,用来发送消息的对象
        JPushClient client=new JPushClient(masterSecret, appKey);
        //创建自定义消息的参数
        CustomMessageParams params = new CustomMessageParams();
        //设置接收类型
        params.setReceiverType(ReceiverTypeEnum.APP_KEY);
        params.setReceiverValue("haha");
        //可以发送通知
               //        MessageResult msgResult =client.sendNotificationAll("这里是测试发送的内容!+++1");
        //也可以发送自定义消息
        MessageResult msgResult=client.sendCustomMessage("这是一条测试的标题""这里是内容部分123", params, null);
         
 
 
如果无意外的话,即可发送一条消息到手机上。 注意:自定义消息需要在客户端的Receiver中自行定义。不会发出提醒。
 
 
 
2014年5月4日14:28:23
 
 
附加内容,自定义通知栏的样式的两种方法:
    
    1、发送自定义消息,自己定义Notification的样式并显示。
    2、发送推送消息。需要用到Jpush包提供的API。
    当用户需要定制默认的通知栏样式时,则可调用此方法。

极光 Push SDK 提供了 2 个用于定制通知栏样式的构建类:

  • BasicPushNotificationBuilder
    • Basic 用于定制 Android Notification 里的 defaults / flags / icon 等基础样式(行为)
  • CustomPushNotificationBuilder
一般使用BasicPushNotificationBuilder即可。
    BasicPushNotificationBuilder builder =  new  BasicPushNotificationBuilder(
        getApplicationContext());
    builder.notificationDefaults = Notification.DEFAULT_LIGHTS;  //只设置了默认闪光灯
    builder.notificationFlags = Notification.FLAG_AUTO_CANCEL// 设置为自动消失
    JPushInterface.setPushNotificationBuilder(1, builder);  //设置 Jpush 的通知栏样式为build,并设置样式编号为1,发送推送时需使用此编号,此方法需在 Jpush 初始化后使用  
    
    因为在业务中需要自定义声音又要求使用推送方式。所以设置一个通知栏样式中声音不设置。在收到推送的事件(Receiver)中播放一段声音。使用SoundPool方式,在应用初始化时初始化一个SoundPoll对象和一个int值
     public static SoundPool sound;
    public static int soundId;
 
初始化对象
        sound = new SoundPool(3,  AudioManager.STREAM_MUSIC, 0);
        soundId = sound.load(getApplicationContext(), R.raw.lingsheng, 1);
 
在Receiver中播放
    if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
        .getAction())) {
        sound.play(MyApp.soundId, 1.0f, 1.0f, 1, 0, 1.0f);//利用初始化后的SoundPool对象播放
    }
 
 
 
 
 
 
 
 
 
 
 





转载于:https://www.cnblogs.com/linwoain/p/470fe36d882644e90b3a221366fcb795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值