react-native怎么清空badge


(zuyue) #1

后台设置badge为1,发送了4条通知,badge显示为4,点击通知后在回调函数里使用JPush.setBadge设置badge为0,但是再次发送通知的时候badge显示为5,要实现点击通知后badge数量减1的效果应该怎么做?


#2

1、你推送的时候设置的badge是+1吗?如果是1那么每次收到都是1
2、你setbadge调用后是否有成功,提供下日志


(zuyue) #3

java sdk的setBadge方法要求传入的是个int,怎么传+1?写了+1也会被当成普通的1处理吧,要是不设置setBadge反而实现了每次收到消息都加1的效果,react-native的setBadge方法传入0执行之后badge不显示了,但是回调函数没有执行,也没有日志


#4

1、用autoBadge或者incrBadge来设置+1
2、你这里不显示了应该是因为调用了本地的所以没显示了,而setbadge是要看回调是不是成功了的


(zuyue) #5

搞不明白,不如不设置


#6

角标你看下这个


(zuyue) #7

真麻烦,还得写oc的代码,rn封个方法能麻烦死么:unamused:


#8

:sweat_smile:这个。。。用我们的插件

https://github.com/jpush/jpush-react-native/blob/master/example/documents/iOS_API.md


(zuyue) #9

用的就是这个啊,setBadge就是个治标不治本的花瓶方法吗?点击通知之后的事件居然还要自行监听,重要的是文档上根本没有提应该监听哪个事件,文档炒鸡简陋,用个方法还得官网git两头折腾,复制粘贴一下会累死几个人??各种槽点满满


#10

额?不理解你说的花瓶方法什么意思,这个是我们提供的设置极光服务器存储的角标的方法呀(当你推送使用+1的时候,这个方法来设置+1的起始值,你下次收到要在多少数上+1就用他设置),这是正经的接口,哪花瓶了,你要治什么本?


(zuyue) #11

都不好使,也不提示为什么不好使


#12

额,这样吧,
1、调用API清除角标
2、然后推送一条消息。
3、把你设置角标、收到这条消息(消息数据里面会有badge的值)的日志给我


(HuminiOS) #13

要实现 减一效果, 在收到 receivenotification 这个事件后 获取 badge 这个字段, 自行减一, 然后调用 setBadge 这个方法


(zuyue) #14

我把服务端的setBadge干掉,然后把rn端的setBadeg方法回调函数里接收到的值alert出来,以前是打印控制台日志的,吓我一跳啊,居然是个true啊,我一直以为是个数字或是啥的,搞得我一直以为这个方法不好使,为什么rn的文档里没说明这个鬼是个true啊,过了一两秒才控制台打印出这个一个鬼:2017-04-14 09:23:52.747560 CWSSurveyIOS[13153:6749354] | JIGUANG | I - [JIGUANGBadgeNumberReport] set badge:0 succeed:expressionless:敢不敢把文档好好改一下,程序媛的小心灵很脆弱的啊喂


#15

这个日志说明你现在客户端 调用setbadge为0成功,即服务端给你存储的badge为0了,那么此时你服务端推送时设置badge+1,客户端收到时就是1,这时如果不做任何处理,你再推送一条消息(设置badge+1),客户端收到的角标就是2。

你所说的true的情况请给日志和截图说明一下。


(zuyue) #16


(zuyue) #17

还有就是ReceiveNotification这个事件在app状态为inactive时并不会触发,只有在active状态时才能触发


(HuminiOS) #18

什么系统?

提供一下 appdelegate.m 代码


(zuyue) #19

IOS10.2

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"
#import <RCTJPushModule.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
 #ifdef NSFoundationVersionNumber_iOS_9_x_Max
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
     entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
     [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
 
#endif
} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                      UIUserNotificationTypeSound |
                                                      UIUserNotificationTypeAlert)
                                          categories:nil];
  } else {
    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                      UIRemoteNotificationTypeSound |
                                                      UIRemoteNotificationTypeAlert)
                                          categories:nil];
  }
  
  [JPUSHService setupWithOption:launchOptions appKey:@"6ddc825d9a8fb821b568fd31"
                        channel:nil apsForProduction:nil];
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"CWSSurveyIOS"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler {
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
 NSDictionary * userInfo = notification.request.content.userInfo;
 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
 [JPUSHService handleRemoteNotification:userInfo];
 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
    }
 completionHandler(UNNotificationPresentationOptionAlert);
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo];
}
completionHandler();
}
@end

(HuminiOS) #20

不知道你是否真的了解 inactivie 的意思,能不能描述何种操作导致 ReceiveNotification 事件不回调