Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-fcm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
ym
react-native-fcm
Commits
b997199c
Commit
b997199c
authored
Nov 28, 2016
by
Libin Lu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use new notification lib
parent
b4a8df2d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
150 additions
and
104 deletions
+150
-104
ios/RNFIRMessaging.h
ios/RNFIRMessaging.h
+0
-2
ios/RNFIRMesssaging.m
ios/RNFIRMesssaging.m
+150
-102
No files found.
ios/RNFIRMessaging.h
View file @
b997199c
#import <UIKit/UIKit.h>
@import
FirebaseAnalytics
;
@import
FirebaseInstanceID
;
@import
FirebaseMessaging
;
#import "RCTBridgeModule.h"
...
...
ios/RNFIRMesssaging.m
View file @
b997199c
...
...
@@ -6,6 +6,7 @@
#import "RCTUtils.h"
@import
UserNotifications
;
@import
FirebaseMessaging
;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
...
...
@@ -21,28 +22,66 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived";
@implementation
RCTConvert
(
NSCalendarUnit
)
+
(
NSCalendarUnit
*
)
NSCalendarUnit
:(
id
)
json
RCT_ENUM_CONVERTER
(
NSCalendarUnit
,
(@{
@"year"
:
@
(
NSCalendarUnitYear
),
@"month"
:
@
(
NSCalendarUnitMonth
),
@"week"
:
@
(
NSCalendarUnitWeekOfYear
),
@"day"
:
@
(
NSCalendarUnitDay
),
@"hour"
:
@
(
NSCalendarUnitHour
),
@"minute"
:
@
(
NSCalendarUnitMinute
)
}),
0
,
integerValue
)
@end
@implementation
RCTConvert
(
UNNotificationRequest
)
+
(
UNNotificationRequest
*
)
UNNotificationRequest
:(
id
)
json
{
NSString
*
key
=
[
self
NSString
:
json
];
if
([
key
isEqualToString
:
@"minute"
]){
return
NSCalendarUnitMinute
;
NSDictionary
<
NSString
*
,
id
>
*
details
=
[
self
NSDictionary
:
json
];
UNMutableNotificationContent
*
content
=
[
UNMutableNotificationContent
new
];
content
.
title
=
[
RCTConvert
NSString
:
details
[
@"title"
]];
content
.
body
=
[
RCTConvert
NSString
:
details
[
@"body"
]];
content
.
sound
=
[
RCTConvert
NSString
:
details
[
@"sound"
]]
?:
[
UNNotificationSound
defaultSound
];
content
.
categoryIdentifier
=
[
RCTConvert
NSString
:
details
[
@"click_action"
]];
content
.
userInfo
=
details
;
content
.
badge
=
[
RCTConvert
NSNumber
:
details
[
@"badge"
]];
NSDate
*
fireDate
=
[
RCTConvert
NSDate
:
details
[
@"fire_date"
]]
?:
[
NSDate
date
];
NSCalendarUnit
interval
=
[
RCTConvert
NSCalendarUnit
:
details
[
@"repeat_interval"
]];
NSCalendarUnit
unitFlags
;
switch
(
interval
)
{
case
NSCalendarUnitMinute
:
{
unitFlags
=
NSCalendarUnitSecond
;
break
;
}
case
NSCalendarUnitHour
:
{
unitFlags
=
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
break
;
}
if
([
key
isEqualToString
:
@"second"
]){
return
NSCalendarUnitSecond
;
case
NSCalendarUnitDay
:
{
unitFlags
=
NSCalendarUnitHour
|
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
break
;
}
if
([
key
isEqualToString
:
@"day"
]){
return
NSCalendarUnitDay
;
case
NSCalendarUnitWeekOfYear
:
{
unitFlags
=
NSCalendarUnitWeekday
|
NSCalendarUnitHour
|
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
break
;
}
if
([
key
isEqualToString
:
@"month"
])
{
return
NSCalendarUnitMonth
;
case
NSCalendarUnitMonth
:
{
unitFlags
=
NSCalendarUnitDay
|
NSCalendarUnitHour
|
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
}
if
([
key
isEqualToString
:
@"week"
])
{
return
NSCalendarUnitWeekOfYear
;
case
NSCalendarUnitYear
:
{
unitFlags
=
NSCalendarUnitMonth
|
NSCalendarUnitDay
|
NSCalendarUnitHour
|
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
}
if
([
key
isEqualToString
:
@"year"
]){
return
NSCalendarUnitYear
;
default:
unitFlags
=
NSCalendarUnitDay
|
NSCalendarUnitMonth
|
NSCalendarUnitYear
|
NSCalendarUnitHour
|
NSCalendarUnitMinute
|
NSCalendarUnitSecond
;
break
;
}
return
0
;
NSDateComponents
*
components
=
[[
NSCalendar
currentCalendar
]
components
:
unitFlags
fromDate
:
fireDate
];
UNCalendarNotificationTrigger
*
trigger
=
[
UNCalendarNotificationTrigger
triggerWithDateMatchingComponents
:
components
repeats
:
interval
!=
0
];
return
[
UNNotificationRequest
requestWithIdentifier
:[
RCTConvert
NSString
:
details
[
@"id"
]]
content
:
content
trigger
:
trigger
];
}
@end
...
...
@@ -54,7 +93,9 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived";
NSDictionary
<
NSString
*
,
id
>
*
details
=
[
self
NSDictionary
:
json
];
UILocalNotification
*
notification
=
[
UILocalNotification
new
];
notification
.
fireDate
=
[
RCTConvert
NSDate
:
details
[
@"fire_date"
]]
?:
[
NSDate
date
];
notification
.
alertTitle
=
[
RCTConvert
NSString
:
details
[
@"title"
]];
if
([
notification
respondsToSelector
:
@selector
(
setAlertTitle
:)]){
[
notification
setAlertTitle
:[
RCTConvert
NSString
:
details
[
@"title"
]]];
}
notification
.
alertBody
=
[
RCTConvert
NSString
:
details
[
@"body"
]];
notification
.
alertAction
=
[
RCTConvert
NSString
:
details
[
@"alert_action"
]];
notification
.
soundName
=
[
RCTConvert
NSString
:
details
[
@"sound"
]]
?:
UILocalNotificationDefaultSoundName
;
...
...
@@ -64,7 +105,6 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived";
notification
.
applicationIconBadgeNumber
=
[
RCTConvert
NSInteger
:
details
[
@"badge"
]];
return
notification
;
}
@end
@implementation
RNFIRMessaging
...
...
@@ -143,52 +183,6 @@ RCT_EXPORT_METHOD(getFCMToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromi
resolve
([[
FIRInstanceID
instanceID
]
token
]);
}
RCT_EXPORT_METHOD
(
getScheduledLocalNotifications
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
NSMutableArray
*
list
=
[[
NSMutableArray
alloc
]
init
];
for
(
UILocalNotification
*
notif
in
[
RCTSharedApplication
()
scheduledLocalNotifications
]){
NSString
*
interval
;
switch
(
notif
.
repeatInterval
){
case
NSCalendarUnitMinute
:
interval
=
@"minute"
;
break
;
case
NSCalendarUnitSecond
:
interval
=
@"second"
;
break
;
case
NSCalendarUnitDay
:
interval
=
@"day"
;
break
;
case
NSCalendarUnitMonth
:
interval
=
@"month"
;
break
;
case
NSCalendarUnitWeekOfYear
:
interval
=
@"week"
;
break
;
case
NSCalendarUnitYear
:
interval
=
@"year"
;
break
;
}
NSMutableDictionary
*
formattedLocalNotification
=
[
NSMutableDictionary
dictionary
];
if
(
notif
.
fireDate
)
{
NSDateFormatter
*
formatter
=
[
NSDateFormatter
new
];
[
formatter
setDateFormat
:
@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
];
NSString
*
fireDateString
=
[
formatter
stringFromDate
:
notif
.
fireDate
];
formattedLocalNotification
[
@"fire_date"
]
=
fireDateString
;
}
formattedLocalNotification
[
@"alert_action"
]
=
RCTNullIfNil
(
notif
.
alertAction
);
formattedLocalNotification
[
@"body"
]
=
RCTNullIfNil
(
notif
.
alertBody
);
formattedLocalNotification
[
@"title"
]
=
RCTNullIfNil
(
notif
.
alertTitle
);
formattedLocalNotification
[
@"badge"
]
=
@
(
notif
.
applicationIconBadgeNumber
);
formattedLocalNotification
[
@"click_action"
]
=
RCTNullIfNil
(
notif
.
category
);
formattedLocalNotification
[
@"sound"
]
=
RCTNullIfNil
(
notif
.
soundName
);
formattedLocalNotification
[
@"repeat_interval"
]
=
RCTNullIfNil
(
interval
);
formattedLocalNotification
[
@"data"
]
=
RCTNullIfNil
(
RCTJSONClean
(
notif
.
userInfo
));
[
list
addObject
:
formattedLocalNotification
];
}
resolve
(
list
);
}
-
(
void
)
onTokenRefresh
{
[
_bridge
.
eventDispatcher
sendDeviceEventWithName
:
@"FCMTokenRefreshed"
body
:[[
FIRInstanceID
instanceID
]
token
]];
...
...
@@ -230,13 +224,6 @@ RCT_EXPORT_METHOD(requestPermissions)
[[
UIApplication
sharedApplication
]
registerForRemoteNotifications
];
}
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// Receive data message on iOS 10 devices.
-
(
void
)
applicationReceivedRemoteMessage
:(
FIRMessagingRemoteMessage
*
)
remoteMessage
{
[
_bridge
.
eventDispatcher
sendDeviceEventWithName
:
FCMNotificationReceived
body
:[
remoteMessage
appData
]];
}
#endif
RCT_EXPORT_METHOD
(
subscribeToTopic
:
(
NSString
*
)
topic
)
{
[[
FIRMessaging
messaging
]
subscribeToTopic
:
topic
];
...
...
@@ -247,29 +234,90 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic)
[[
FIRMessaging
messaging
]
unsubscribeFromTopic
:
topic
];
}
RCT_EXPORT_METHOD
(
presentLocalNotification
:
(
UILocalNotification
*
)
notification
)
// Receive data message on iOS 10 devices.
-
(
void
)
applicationReceivedRemoteMessage
:(
FIRMessagingRemoteMessage
*
)
remoteMessage
{
[
_bridge
.
eventDispatcher
sendDeviceEventWithName
:
FCMNotificationReceived
body
:[
remoteMessage
appData
]];
}
RCT_EXPORT_METHOD
(
presentLocalNotification
:
(
UNNotificationRequest
*
)
data
resolver
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
[
RCTSharedApplication
()
presentLocalNotificationNow
:
notification
];
if
([
UNUserNotificationCenter
currentNotificationCenter
]
!=
nil
){
UNNotificationRequest
*
request
=
[
RCTConvert
UNNotificationRequest
:
data
];
[[
UNUserNotificationCenter
currentNotificationCenter
]
addNotificationRequest
:
request
withCompletionHandler
:
^
(
NSError
*
_Nullable
error
)
{
if
(
!
error
)
{
resolve
(
nil
);
}
else
{
reject
(
@"notification_error"
,
@"Failed to present local notificaton"
,
error
);
}
}];
}
else
{
UILocalNotification
*
notif
=
[
RCTConvert
UILocalNotification
:
data
];
[
RCTSharedApplication
()
presentLocalNotificationNow
:
notif
];
resolve
(
nil
);
}
}
RCT_EXPORT_METHOD
(
scheduleLocalNotification
:
(
UILocalNotification
*
)
notification
)
RCT_EXPORT_METHOD
(
scheduleLocalNotification
:
(
id
)
data
resolver
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
[
RCTSharedApplication
()
scheduleLocalNotification
:
notification
];
if
([
UNUserNotificationCenter
currentNotificationCenter
]
!=
nil
){
UNNotificationRequest
*
request
=
[
RCTConvert
UNNotificationRequest
:
data
];
[[
UNUserNotificationCenter
currentNotificationCenter
]
addNotificationRequest
:
request
withCompletionHandler
:
^
(
NSError
*
_Nullable
error
)
{
if
(
!
error
)
{
resolve
(
nil
);
}
else
{
reject
(
@"notification_error"
,
@"Failed to present local notificaton"
,
error
);
}
}];
}
else
{
UILocalNotification
*
notif
=
[
RCTConvert
UILocalNotification
:
data
];
[
RCTSharedApplication
()
scheduleLocalNotification
:
notif
];
resolve
(
nil
);
}
}
RCT_EXPORT_METHOD
(
cancelAllLocalNotifications
)
{
if
([
UNUserNotificationCenter
currentNotificationCenter
]
!=
nil
){
[[
UNUserNotificationCenter
currentNotificationCenter
]
removeAllPendingNotificationRequests
];
[[
UNUserNotificationCenter
currentNotificationCenter
]
removeAllDeliveredNotifications
];
}
else
{
[
RCTSharedApplication
()
cancelAllLocalNotifications
];
}
}
RCT_EXPORT_METHOD
(
cancelLocalNotification
:
(
NSString
*
)
notificationId
)
{
if
([
UNUserNotificationCenter
currentNotificationCenter
]
!=
nil
){
[[
UNUserNotificationCenter
currentNotificationCenter
]
removePendingNotificationRequestsWithIdentifiers
:@[
notificationId
]];
}
else
{
for
(
UILocalNotification
*
notification
in
[
UIApplication
sharedApplication
].
scheduledLocalNotifications
)
{
NSDictionary
<
NSString
*
,
id
>
*
notificationInfo
=
notification
.
userInfo
;
if
([
notificationId
isEqualToString
:[
notificationInfo
valueForKey
:
@"id"
]]){
[[
UIApplication
sharedApplication
]
cancelLocalNotification
:
notification
];
}
}
}
}
RCT_EXPORT_METHOD
(
getScheduledLocalNotifications
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
if
([
UNUserNotificationCenter
currentNotificationCenter
]
!=
nil
){
[[
UNUserNotificationCenter
currentNotificationCenter
]
getPendingNotificationRequestsWithCompletionHandler
:
^
(
NSArray
<
UNNotificationRequest
*>
*
_Nonnull
requests
)
{
NSMutableArray
*
list
=
[[
NSMutableArray
alloc
]
init
];
for
(
UNNotificationRequest
*
notif
in
requests
){
NSString
*
interval
;
UNMutableNotificationContent
*
content
=
notif
.
content
;
[
list
addObject
:
content
.
userInfo
];
}
resolve
(
list
);
}];
}
else
{
NSMutableArray
*
list
=
[[
NSMutableArray
alloc
]
init
];
for
(
UILocalNotification
*
notif
in
[
RCTSharedApplication
()
scheduledLocalNotifications
]){
[
list
addObject
:
notif
.
userInfo
];
}
resolve
(
list
);
}
}
RCT_EXPORT_METHOD
(
setBadgeNumber
:
(
NSInteger
*
)
number
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment