集成阿里云推送

项目中有需要用到推送,让用的是阿里的推送,不得不说官方文档写的不是那么详细,使用的时候还是遇到了一些问题。
首先去集成sdk
下载地址https://develop.aliyun.com/tools/sdk?spm=5176.136436.698463.16.60541cb7wf6iZb#/android
grade直接
//阿里推送
compile ‘com.aliyun.ams:alicloud-android-push:3.0.11@aar’
// compile ‘com.aliyun.ams:alicloud-android-utdid:1.1.5.3’
// compile ‘com.aliyun.ams:alicloud-android-ut:5.1.0’(我还集成了支付功能,资源重复)

在manifest里配置自己的appkey和appsecret
然后需要MessageReceiver和MessageIntentService,可以直接复制官方的,文档上是写MessageReceiver,但功能不能实现浪费了一些时间。
public class MyMessageIntentService extends AliyunMessageIntentService {
private static final String TAG = “MyMessageIntentService”;

/**
 * 推送通知的回调方法
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
protected void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
    Log.i(MessageReceiver.REC_TAG,"收到一条推送通知 : " + title + ", summary:" + summary);

}

/**
 * 推送消息的回调方法
 * @param context
 * @param cPushMessage
 */
@Override
protected void onMessage(Context context, CPushMessage cPushMessage) {
    Log.i(MessageReceiver.REC_TAG,"收到一条推送消息 : " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());

}

/**
 * 从通知栏打开通知的扩展处理
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
protected void onNotificationOpened(Context context, String title, String summary, String extraMap) {
    Log.i(MessageReceiver.REC_TAG,"onNotificationOpened : " + " : " + title + " : " + summary + " : " + extraMap);

}

/**
 * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
    Log.i(MessageReceiver.REC_TAG,"onNotificationClickedWithNoAction : " + " : " + title + " : " + summary + " : " + extraMap);

}

/**
 * 通知删除回调
 * @param context
 * @param messageId
 */
@Override
protected void onNotificationRemoved(Context context, String messageId) {
    Log.i(MessageReceiver.REC_TAG, "onNotificationRemoved : " + messageId);

}

/**
 * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html#h3-3-4-basiccustompushnotification-api
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 * @param openType
 * @param openActivity
 * @param openUrl
 */
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
    Log.i(MessageReceiver.REC_TAG,"onNotificationReceivedInApp : " + " : " + title + " : " + summary + "  " + extraMap + " : " + openType + " : " + openActivity + " : " + openUrl);

}

}

public class MessageReceiver extends com.alibaba.sdk.android.push.MessageReceiver {
// 消息接收部分的LOG_TAG
public static final String REC_TAG = “receiver”;

/**
 * 推送通知的回调方法
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
    // TODO 处理推送通知
    if ( null != extraMap ) {
        for (Map.Entry<String, String> entry : extraMap.entrySet()) {
            Log.i(REC_TAG,"@Get diy param : Key=" + entry.getKey() + " , Value=" + entry.getValue());
        }
    } else {
        Log.i(REC_TAG,"@收到通知 && 自定义消息为空");
    }
    Log.i(REC_TAG,"收到一条推送通知 : " + title + ", summary:" + summary);
}

/**
 * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html?spm=5176.product30047.6.620.wjcC87#h3-3-4-basiccustompushnotification-api
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 * @param openType
 * @param openActivity
 * @param openUrl
 */
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
    Log.i(REC_TAG,"onNotificationReceivedInApp : " + " : " + title + " : " + summary + "  " + extraMap + " : " + openType + " : " + openActivity + " : " + openUrl);
   // MainApplication.setConsoleText("onNotificationReceivedInApp : " + " : " + title + " : " + summary);
}

/**
 * 推送消息的回调方法
 * @param context
 * @param cPushMessage
 */
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
    Log.i(REC_TAG,"收到一条推送消息 : " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    CommonUtils.showToast(context,"收到一条推送消息 : " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());

}

/**
 * 从通知栏打开通知的扩展处理
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
    Log.i(REC_TAG,"onNotificationOpened : " + " : " + title + " : " + summary + " : " + extraMap);
   // MainAppcalation.setConsoleText("onNotificationOpened : " + " : " + title + " : " + summary + " : " + extraMap);
}

/**
 * 通知删除回调
 * @param context
 * @param messageId
 */
@Override
public void onNotificationRemoved(Context context, String messageId) {
    Log.i(REC_TAG, "onNotificationRemoved : " + messageId);
}

/**
 * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened
 * @param context
 * @param title
 * @param summary
 * @param extraMap
 */
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
    Log.i(REC_TAG,"onNotificationClickedWithNoAction : " + " : " + title + " : " + summary + " : " + extraMap);
}

}
需要manifest配置一些信息,这里就不写了

//注册阿里云
private CloudPushService mPushService = PushServiceFactory.getCloudPushService();
mPushService.bindAccount(“你要绑定的账号”, new CommonCallback() {
@Override
public void onSuccess(String s) {
String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
// CommonUtils.showToast(context, deviceId);
Log.i(“deviceId”, deviceId);
}

                                @Override
                                public void onFailed(String s, String s1) {
                                    CommonUtils.showToast(context, "onFailed" + s);
                                }
                            });

最后不要忘了解绑
mPushService.unbindAccount(new CommonCallback() {
@Override
public void onSuccess(String s) {

                }

                @Override
                public void onFailed(String s, String s1) {

                }
            });

猜你喜欢

转载自blog.csdn.net/guldanhu/article/details/78716858
今日推荐