Pushdy
示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行pod install
。
要求
Swift >= 4.2
安装
Pushdy可通过CocoaPods获得。安装它,只需将以下行添加到Podfile中:
pod 'PushdySDK'
用法
导入
在 Swift 语言中导入模块
import PushdySDK
在 Objective-C 语言中导入模块(您必须首先生成桥接头以支持 Swift)
#import <PushdySDK/PushdySDK-Swift.h>
初始化
在 application:didFinishLaunchingWithOptions 方法中,按如下方式初始化 Pushdy SDK:
// Swift language
let clientKey = "your client key from Pushdy application"
Pushdy.initWith(clientKey: clientKey, delegate: self, launchOptions: launchOptions)
// Objective-C language
NSString *clientKey = @"your client key from Pushdy application";
[Pushdy initWithClientKey:clientKey delegate:self launchOptions:launchOptions];
然后您可以调用 registerForPushNotifications 方法来注册接收推送通知。
// Swift language
Pushdy.registerForPushNotifications()
// Objective-C language
[Pushdy registerForPushNotifications];
方法
- getDeviceToken
从 Pushdy 获取设备令牌
// Swift language
Pushdy.getDeviceToken()
// Objective-C language
[Pushdy getDeviceToken];
- checkNotificationEnabling
检查是否允许通知
// Swift language
Pushdy.checkNotificationEnabling { (enabled:Bool) in
}
// Objective-C language
[Pushdy checkNotificationEnabling:^(BOOL enabled) {
}];
- setDeviceID
使用您的设备 ID 而不是 Pushdy 设备 ID
// Swift language
let yourDeviceID = ...
Pushdy.setDeviceID(yourDeviceID)
// Objective-C language
NSString* yourDeviceID = ...;
[Pushdy setDeviceID:yourDeviceID];
- getPendingNotification
获取尚未处理的挂起通知
// Swift language
Pushdy.getPendingNotification()
// Objective-C language
[Pushdy getPendingNotification];
- setAttribute
为属性设置值。您可以设置 "commitImmediately" 变量为 true 立即提交您的值。
// Swift language
try? Pushdy.setAttribute("", value: "")
// Equivalent to
try? Pushdy.setAttribute("network_carrier", value: "your_network_carrier", commitImmediately: false)
// Objective-C language
[Pushdy setAttribute:@"network_carrier" value:@"your_network_carrier" error:nil];
// Equivalent to
[Pushdy setAttribute:@"network_carrier" value:@"your_network_carrier" commitImmediately:FALSE error:nil];
- pushAttribute
将值推送到数组属性的一种类型。您可以设置 "commitImmediately" 变量为 true 立即提交您的值。
// Swift language
let books:[String] = [
"book_1",
"book_2"
]
try? Pushdy.pushAttribute("bought_books", value: books)
// Equivalent to
try? Pushdy.pushAttribute("bought_books", value: books, commitImmediately: false)
// Objective-C language
NSArray *books = @[@"book_1", @"book_2"];
[Pushdy pushAttribute:@"bought_books" value:books error:nil];
// Equivalent to
[Pushdy pushAttribute:@"bought_books" value:books commitImmediately:FALSE error:nil];
Pushdy 委托
为了监听 Pushdy 回调和根据 Pushdy 适配您的逻辑,您必须在与 App Delegate 中实现 PushdyDelegate
// Swift language
import PushdySDK
class AppDelegate: UIResponder, UIApplicationDelegate, PushdyDelegate {
}
// Objective-C language
#import <PushdySDK/PushdySDK-Swift.h> // You must generate brigde header first
@interface AppDelegate : UIResponder <UIApplicationDelegate, PushdyDelegate> {
}
-readyForHandlingNotification
确定应用是否可以处理推送通知。默认为 true。如果为 false,则传入的推送将推送到挂起通知,您可以稍后处理挂起通知。
// Swift language
func readyForHandlingNotification() -> Bool {
var already = true
// Example: already = pass through login or tutorial/introdution screen
return already
}
// Objective-C language
- (BOOL)readyForHandlingNotification {
BOOL already = YES;
// Example: already = pass through login or tutorial/introduction screen
return already;
}
-onNotificationReceived:fromState
当应用收到通知时,Pushdy 将触发此方法。
// Swift language
func onNotificationReceived(_ notification: [String : Any], fromState: String) {
if fromState == "not_running" {
// Example: is_app_launched_from_push = true
}
else if fromState == "active" {
// Example: Play a sound to notitfy user
}
else if fromState == "inactive" {
// Example: Play a sound to notitfy user
}
else if fromState == "background" {
}
}
// Objective-C language
- (void)onNotificationReceived:(NSDictionary<NSString *,id> *)notification fromState:(NSString *)fromState {
if ([fromState isEqualToString:@"not_running"]) {
// Example: is_app_launched_from_push = true
}
else if ([fromState isEqualToString:@"active"]) {
// Example: Play a sound to notitfy user
}
else if ([fromState isEqualToString:@"inactive"]) {
// Example: Play a sound to notitfy user
}
else if ([fromState isEqualToString:@"background"]) {
}
}
-onNotificationOpened:fromState
当用户点击推送通知横幅(系统通知或在应用通知横幅)时,Pushdy 将触发此方法。
// Swift language
func onNotificationOpened(_ notification: [String : Any], fromState: String) {
// Handle notification
}
// Objective-C language
- (void)onNotificationOpened:(NSDictionary<NSString *,id> *)notification fromState:(NSString *)fromState {
// Handle notification
}
以及其他委托方法...
自定义 In App 通知横幅
我们使用 PDYNotificationView 视图进行默认的应用内推送通知显示。Pushdy 还提供了一些方法来调整默认通知视图并设置您的自定义视图。
- setPushBannerAutoDismiss
打开/关闭应用内通知横幅的自动消失。
// Swift language
Pushdy.setPushBannerAutoDismiss(true)
// Objective-C language
[Pushdy setPushBannerAutoDismiss:TRUE];
- setPushBannerDismissDuration
设置默认自定义视图的自动消失持续时间。
// Swift language
Pushdy.setPushBannerDismissDuration(5) // 5 seconds
// Objective-C language
[Pushdy setPushBannerDismissDuration:5]; // 5 seconds
- setCustomPushBanner
设置自定义通知横幅视图。需要实现 PDYPushBannerActionProtocol 协议。
// Swift language
let yourCustomView = ...
Pushdy.setCustomPushBanner(yourCustomView)
// Objective-C language
UIView* yourCustomView = ...
[Pushdy setCustomPushBanner:yourCustomView];
*** 注意
Pushdy SDK 使用 _nms_image
键作为默认显示从 json 推送有效负载中的缩略图图像。
{
"aps" : {
...
},
"_nms_image" : "https://domain.com/path/image.png"
}
如果您想自定义您自己的键,请使用 setCustomMediaKey 方法来覆盖它。
// Swift language
PDYNotificationView.setCustomMediaKey("your_custom_media_key")
// Objective-C language
[PDYNotificationView setCustomMediaKey:@"your_custom_media_key"];
作者
Pushdy 团队,[email protected]
许可协议
Pushdy 依据 MIT 许可协议提供。更多信息请参见 LICENSE 文件。