极光推送推送

Android极光推送使用方法大全(升级JCore为2.0版本)

2018-04-03  本文已影响0人  Wang_Mr
创建应用

进入极光控制台后,点击“创建应用”按钮,进入创建应用的界面。 填上你的应用程序的名称以及应用包名这二项就可以了, 最后点击最下方的 “创建我的应用”按钮,创建应用完毕。

创建应用
填写应用程序的名称以及上传图标
创建成功
添加应用包名
jcenter 自动集成步骤
buildscript {
    repositories {
        jcenter()
    }
    ......
}

allprojets {
    repositories {
        jcenter()
    }
}

android {
    ......
    defaultConfig {
        applicationId "com.xxx.xxx" //JPush上注册的包名.
        ......

        ndk {
            //选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
        }

        manifestPlaceholders = [
            JPUSH_PKGNAME : applicationId,
            JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
            JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
        ]
        ......
    }
    ......
}

dependencies {
    ......

    implementation 'cn.jiguang.sdk:jpush:3.3.4'  // 此处以JPush 3.3.4 版本为例。
    implementation 'cn.jiguang.sdk:jcore:2.1.0'  // 此处以JCore 2.1.0 版本为例。
    ......
}
public class XService extends JCommonService {

}
    <application
        ...>

        <!-- 注册继承JCommonService的服务 -->
        <service android:name=".XService"
            android:enabled="true"
            android:exported="false"
            android:process=":pushcore">
            <intent-filter>
                <action android:name="cn.jiguang.user.service.action" />
            </intent-filter>
        </service>
        
        <!-- 替换原生极光推送接收器 -->
        <receiver
            android:name=".jpush.MyReceiver"
            android:enabled="true"
            android:exported="false"
            tools:node="replace">
            <intent-filter>
                <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
                <category android:name="com.xxx.xxx" /> <!--JPush上注册的包名 --> 
            </intent-filter>
        </receiver>
    </application>
调试以及使用
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
public class MyReceiver extends JPushMessageReceiver {

    private static final String TAG = "JIGUANG";

    /**
     * TODO 连接极光服务器
     */
    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
        Log.e(TAG, "onConnected");
    }
    
    /**
     * TODO 注册极光时的回调
     */
    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
        Log.e(TAG, "onRegister" + s);
    }
    
    /**
     * TODO 注册以及解除注册别名时回调
     */
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
        Log.e(TAG, jPushMessage.toString());
    }

    /**
     * TODO 接收到推送下来的通知
     *      可以利用附加字段(notificationMessage.notificationExtras)来区别Notication,指定不同的动作,附加字段是个json字符串
     *      通知(Notification),指在手机的通知栏(状态栏)上会显示的一条通知信息
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
        Log.e(TAG, notificationMessage.toString());
    }

    /**
     * TODO 打开了通知
     *      notificationMessage.notificationExtras(附加字段)的内容处理代码
     *      比如打开新的Activity, 打开一个网页等..
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
        Log.e(TAG, notificationMessage.notificationExtras);
    }


    /**
     * TODO 接收到推送下来的自定义消息
     *      自定义消息不是通知,默认不会被SDK展示到通知栏上,极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。
     *      自定义消息主要用于应用的内部业务逻辑和特殊展示需求
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        Log.e(TAG, "onMessage");
    }
}

// 一般登录之后调用此方法设置别名
// sequence 用来标识一次操作的唯一性(退出登录时根据此参数删除别名)
// alias 设置有效的别名
// 有效的别名组成:字母(区分大小写)、数字、下划线、汉字、特殊字符@!#$&*+=.|。限制:alias 命名长度限制为 40 字节。
JPushInterface.setAlias(context,  int sequence, String alias);
// 退出登录删除别名
JPushInterface.deleteAlias(Context context,int sequence);

可通过onMessage(收到自定义消息这个方法里去定义Notification,而不是用发送通知)

...

@Override
public void onMessage(Context context, CustomMessage customMessage) {
    super.onMessage(context, customMessage);
    // 收到消息 显示通知
    processCustomMessage(context, customMessage.extra);
}
    private void processCustomMessage(Context context, String message) {

        String channelID = "1";
        String channelName = "channel_name";

        // 跳转的Activity
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        //适配安卓8.0的消息渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(context, channelID);

        notification.setAutoCancel(true)
                .setContentText(message)
                .setContentTitle("我是Title")
                .setSmallIcon(R.drawable.logo120)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pendingIntent);

        notificationManager.notify((int)(System.currentTimeMillis()/1000), notification.build());
    }
上一篇 下一篇

猜你喜欢

热点阅读