Android集成友盟推送功能

友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析、社交分享、消息推送、自动更新、在线参数、移动推广效果分析、微社区等app开发和运营解决方案。

如何快速集成友盟推送功能:

1. 注册友盟账号

友盟开发者账号的注册地址:http://www.umeng.com/users/sign_up

2. 创建推送应用

首先进入友盟消息推送的应用中心,创建一个应用,链接地址为:http://message.umeng.com/appList

应用创建完成后,点击应用名称进入应用详情页面,进入“应用信息”页面,可以看到应用的AppKey和AppMasterSecret


3. 下载Android SDK

友盟消息推送android SDK下载地址:

http://dev.umeng.com/push/ios/sdk-download

4. 集成开发

参考http://dev.umeng.com/push/android/integration进行集成开发。

4.1 导入SDK所需的jar包

导入jar包的过程中,如果是ADT 17或者以上的版本,只需要将com.umeng.message.lib.jar直接复制到项目的libs下面,这样做之后,ADT能自动找到所有在libs文件夹下的JAR包,把他们加到工程的依赖路径中,并直接显示在Android Dependecies分类下。

切记,右键Properties -> Java Build Path -> Libraries然后点击Add External JARs... 选择指向jar的路径,点击OK,这种方式导入后,会出现异常,具体原因请参考http://blog.csdn.net/luckily01/article/details/8566956

4.2 配置AndroidManifest.xml,添加相应的权限


4.3 添加组件

在application标签下,注意将[应用包名]换成自己的应用的包名:

<receiver
    android:name="com.umeng.message.NotificationProxyBroadcastReceiver"
    android:exported="false" >
</receiver>
<receiver android:name="com.umeng.message.RegistrationReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED" />

        <data android:scheme="package" />
    </intent-filter>

 
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>

</receiver>
<receiver android:name="com.umeng.message.UmengBroadcastReceiver" >
    <intent-filter>
        <action android:name="org.agoo.android.intent.action.RECEIVE" />
    </intent-filter>
    <intent-filter>
        <action android:name="【应用包名】.intent.action.COMMAND" />
    </intent-filter>
    <intent-filter>
        <action android:name="org.agoo.android.intent.action.RE_ELECTION_V2" />
    </intent-filter>
</receiver>
<receiver android:name="com.umeng.message.BootBroadcastReceiver" >
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>
可以根据需要自行设置 android:label 中的服务名 :
<service
    android:name="com.umeng.message.UmengService"
   android:label="PushService" 
    android:exported="true" 
    android:process=":pushService_v1" >
    <intent-filter>
        <action android:name="【应用包名】.intent.action.START" />
    </intent-filter>
    <intent-filter>
        <action android:name="【应用包名】.intent.action.COCKROACH" />
    </intent-filter>
    <intent-filter>
        <action android:name="org.agoo.android.intent.action.PING" />
    </intent-filter>
</service>
<service
    android:name="org.android.agoo.service.ElectionService"
    android:exported="true"
    android:process=":pushService_v1" >
    <intent-filter>
        <action android:name="org.agoo.android.intent.action.ELECTION_V2" />
    </intent-filter>
</service>
<service android:name="com.umeng.message.UmengIntentService" />
<!-- V1.3.0添加的service,负责下载通知的资源 -->
<service android:name="com.umeng.message.UmengDownloadResourceService" />

4.4 添加AppKey和Umeng Message Secret

<application>标签下:

<meta-data
    android:name="UMENG_APPKEY"
    android:value="你的APPKEY" >
</meta-data>
<meta-data
    android:name="UMENG_MESSAGE_SECRET"
    android:value="你的Umeng Message Secret" >
</meta-data>

 
 

5. 添加代码,编译测试

5.1 在应用的主Activity onCreate() 函数中开启推送服务

PushAgent mPushAgent = PushAgent.getInstance(context);
mPushAgent.enable();
5.2 获取设备的Device Token(可选)

如果在测试或其他使用场景中,需要获取设备的Device Token,可以使用下面的方法。

String device_token = UmengRegistrar.getRegistrationId(context)

说明

  • Device Token为友盟生成的用于标识设备的id,长度为44位,不能定制和修改。同一台设备上每个应用对应的Device Token不一样。
  • 获取Device Token的代码需要放在mPushAgent.enable();后面,注册成功以后调用才能获得Device Token。
  • 如果返回值为空, 说明设备还没有注册成功, 需要等待几秒钟,同时请确保测试手机网络畅通。
5.3 添加测试设备


Device Token可以通过5.2的步骤获取

5.4 发送测试消息

在测试消息下新建测试消息:


确保测试设备网络畅通后,消息推送成功:


测试成功!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
友盟推送集成Spring Boot可以通过友盟提供的Java SDK来实现。具体步骤如下: 1.在pom.xml文件中添加友盟推送的Java SDK依赖: ```xml <dependency> <groupId>com.umeng</groupId> <artifactId>umeng-message</artifactId> <version>1.3.2</version> </dependency> ``` 2.在application.properties文件中添加友盟推送的配置信息: ```properties # 友盟推送配置 umeng.appkey=your_appkey umeng.appMasterSecret=your_app_master_secret umeng.productionMode=false ``` 3.编写友盟推送的Java代码: ```java import com.alibaba.fastjson.JSONObject; import com.umeng.message.*; import com.umeng.message.common.inter.ITagManager; import com.umeng.message.entity.UMessage; import com.umeng.message.tag.TagManager; import java.util.List; public class UmengPushService implements UmengNotificationService { private final AndroidNotification androidNotification; private final IOSNotification iosNotification; private final PushClient client; public UmengPushService(String appKey, String appMasterSecret, boolean productionMode) { androidNotification = new AndroidNotification(); iosNotification = new IOSNotification(); client = new PushClient(); client.setAppKey(appKey); client.setAppMasterSecret(appMasterSecret); client.setProductionMode(productionMode); } @Override public void sendUnicast(String deviceToken, String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setDeviceToken(deviceToken); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void sendListcast(List<String> deviceTokens, String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setDeviceToken(deviceTokens); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void sendBroadcast(String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void addTag(String deviceToken, String tag) throws Exception { ITagManager tagManager = new TagManager(); tagManager.add(deviceToken, tag); } @Override public void deleteTag(String deviceToken, String tag) throws Exception { ITagManager tagManager = new TagManager(); tagManager.delete(deviceToken, tag); } } ``` 4.在Spring Boot中使用友盟推送: ```java @RestController @RequestMapping("/push") public class PushController { @Autowired private UmengNotificationService umengNotificationService; @PostMapping("/unicast") public void unicast(@RequestParam String deviceToken, @RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendUnicast(deviceToken, title, text, extra); } @PostMapping("/listcast") public void listcast(@RequestParam List<String> deviceTokens, @RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendListcast(deviceTokens, title, text, extra); } @PostMapping("/broadcast") public void broadcast(@RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendBroadcast(title, text, extra); } @PostMapping("/addTag") public void addTag(@RequestParam String deviceToken, @RequestParam String tag) throws Exception { umengNotificationService.addTag(deviceToken, tag); } @PostMapping("/deleteTag") public void deleteTag(@RequestParam String deviceToken, @RequestParam String tag) throws Exception { umengNotificationService.deleteTag(deviceToken, tag); } } ``` 以上代码演示了如何在Spring Boot中使用友盟推送,包括单播、列播、广播和添加/删除标签等操作。需要注意的是,以上代码仅供参考,具体实现需要根据自己的业务需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值