MMPReactiveNotification 是一个响应式库,提供本地和远程推送通知的信号。
功能
MMPReactiveNotification 通过 CocoaPods 提供,要安装它,只需将以下行添加到您的 Podfile 中
pod 'MMPReactiveNotification'
在您的应用程序中任何地方使用 MMPReactiveNotification
类方法 service
订阅适当的信号。以下示例展示了如何使用 remoteRegistration
信号以默认设置注册远程推送通知并接收推送令牌
// import the header
#import <MMPReactiveNotification/MMPReactiveNotification.h>
[[[MMPReactiveNotification service]
remoteRegistration]
subscribeNext:^(NSData *tokenData) {
NSLog(@"Receiving push token: %@", tokenData);
// Send the push token to your server
}
error:^(NSError *error) {
NSLog(@"Push registration error: %@", error);
}];
要接收远程推送通知,使用 remoteNotifications
方法
[[[MMPReactiveNotification service]
remoteNotifications]
subscribeNext:^(NSDictionary *pushData) {
NSLog(@"Receiving push: %@", pushData);
}];
要接收本地通知,使用 localNotifications
方法
[[[MMPReactiveNotification service]
localNotifications]
subscribeNext:^(UILocalNotification *localNotification) {
NSLog(@"Receiving local notification: %@", localNotification.alertBody);
}];
远程推送注册的默认设置如下
要自定义这些设置,请使用以下示例中使用 notificationTypes
和 categories
方法
// only enable alert and badge
[[[[MMPReactiveNotification service]
notificationTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge]
remoteRegistration]
subscribeNext:^(NSData *tokenData) {
NSLog(@"Receiving push token: %@", tokenData);
// Send the push token to your server
}];
要创建本地通知,首先使用新的 MMPLocalNotificationSpec
指定通知,然后调用 schedule
方法来安排它
[[[[[[MMPLocalNotificationSpec new]
withAlertBody:@"Your daily quiz is now available!"]
withSoundName:UILocalNotificationDefaultSoundName] withCategory:@"Quiz"]
fireDailyAtHour:18 minute:0 second:0]
schedule];
此安排还将自动注册通知设置,如果之前尚未完成。
MMPReactiveNotification 由 Mamad Purbo 维护。
MMPReactiveNotification 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。