友盟推送集成基本使用

第一次使用友盟的推送,简单的整理一下集成及使用的过程。

集成之前, 请在http://push.umeng.com/申请开通【友盟+】消息推送服务,创建应用获得AppKey和AppSecret,并且将配置好的.p12推送证书(开发、生产)上传。

1.下载SDK http://dev.umeng.com/push/ios/sdk-download

2.把SDK文件夹UMessage_Sdk_1.5.0a拖入工程目录结构中,在弹出的界面中记得勾选Copy items if needed

3.点击项目-> TARGET-> Build Phases-> Link Binary With Libraries->左下方的+号->搜索UserNotifications->选中UserNotifications.framework->点击Add

4.点击项目-> TARGET-> Capabilities,打开Push Notification的开关,编译下项目如果没有错继续下面

5.来到AppDelegate.m,导入头文件#import"UMessage.h",#import<UserNotifications/UserNotifications.h>

6.设置代理<UNUserNotificationCenterDelegate>

7.didFinishLaunchingWithOptions:方法里设置代码如下:

[UMessage startWithAppkey:@"你自己申请的Appkey "launchOptions:launchOptions];


[UMessage registerForRemoteNotifications];


//iOS10必须加下面这段代码。


UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];


center.delegate=self;


UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;


[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOLgranted,NSError*_Nullableerror) {


if(granted) {


//点击允许


}else{


//点击不允许


}


 }];


//打开日志,方便调试


[UMessage setLogEnabled:YES];


8.didRegisterForRemoteNotificationsWithDeviceToken:方法中获取deviceToken,代码如下

- (void)application:(UIApplication*)application


didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{


NSString*token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">"withString:@""] stringByReplacingOccurrencesOfString:@" "withString:@""];
//保存token登录时传给后台
NSLog(@"token==%@",token);


}


9.iOS10以下接收通知的代码

- (void)application:(UIApplication*)application


didReceiveRemoteNotification:(NSDictionary*)userInfo{


//关闭友盟自带的弹出框


[UMessage setAutoAlert:NO];


[UMessage didReceiveRemoteNotification:userInfo];


NSString *body =@"";


NSString *title =@"";


if([[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] isKindOfClass:[NSDictionary class]]) {


NSDictionary *pushName = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];


body = pushName[@"body"];


title = pushName[@"title"];


}


在前台定制自定的的弹出框(看需求)


if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)


{


//可以发送通知做自己需要的事情


[[NSNotificationCenter defaultCenter] postNotificationName:@"userInfoNotification"object:self userInfo:userInfo];


UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:title message:body delegate:self cancelButtonTitle:@"稍后" otherButtonTitles:@"立即查看",nil];


//点击立即查看可以处理跳转到指定需求界面的逻辑


[alertView show];


return;


}else if(application.applicationState == UIApplicationStateInactive){


//从后台点击进来处理跳转界面的逻辑(看需求)


}


}


10.iOS10以上接收通知的代码

//iOS10新增:处理前台收到通知的代理方法


- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{


NSDictionary* userInfo = notification.request.content.userInfo;


if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {


//应用处于前台时的远程推送接受,可以发送通知来刷新新订单等,看自己需求。不需要可以什么都不做


[[NSNotificationCenter defaultCenter] postNotificationName:@"userInfoNotification" object:self userInfo:userInfo];


//关闭U-Push自带的弹出框


[UMessage setAutoAlert:NO];


//必须加这句代码


[UMessage didReceiveRemoteNotification:userInfo];


}else{


//应用处于前台时的本地推送接受


}


//当应用处于前台时提示设置,需要哪个可以设置哪一个


completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);


}


//iOS10新增:处理后台点击通知的代理方法


- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)(void))completionHandler{


NSDictionary* userInfo = response.notification.request.content.userInfo;


if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {


//应用处于后台时的远程推送接受


if(!self.remoteNotification) {//应用没有杀死时调用下面发送通知消息做事


[[NSNotificationCenter defaultCenter] postNotificationName:@"userInfoNotification" object:self userInfo:userInfo];


//在这可以处理界面跳转


}


//必须加这句代码


[UMessage didReceiveRemoteNotification:userInfo];


}else{


//应用处于后台时的本地推送接受


}


}


最后在友盟控制台添加测试设备,在测试模式下发消息就可以调试了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
友盟推送集成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中使用友盟推送,包括单播、列播、广播和添加/删除标签等操作。需要注意的是,以上代码仅供参考,具体实现需要根据自己的业务需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值