Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit 137f1a1

Browse files
committed
Add new methods for requesting iOS permission and deprecate old one
1 parent a5b1ac9 commit 137f1a1

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ If you want to show foreground notifications, add the [relevant configuration](h
219219
```js
220220
import { BatchPush } from '@bam.tech/react-native-batch';
221221

222-
BatchPush.registerForRemoteNotifications();
222+
// when you want to show the alert asking for permission
223+
BatchPush.requestNotificationAuthorization();
224+
225+
// at each app launch (after opt-in if you're opted out by default)
226+
BatchPush.refreshToken();
223227
```
224228

225229
<hr>

ios/RNBatch.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ - (NSDictionary*) dictionaryWithEventDispatcherPayload:(id<BatchEventDispatcherP
166166
[BatchPush registerForRemoteNotifications];
167167
}
168168

169+
RCT_EXPORT_METHOD(push_requestNotificationAuthorization)
170+
{
171+
[BatchPush requestNotificationAuthorization];
172+
}
173+
174+
RCT_EXPORT_METHOD(push_requestProvisionalNotificationAuthorization)
175+
{
176+
[BatchPush requestProvisionalNotificationAuthorization];
177+
}
178+
179+
RCT_EXPORT_METHOD(push_refreshToken)
180+
{
181+
[BatchPush refreshToken];
182+
}
183+
169184
RCT_EXPORT_METHOD(push_clearBadge)
170185
{
171186
[BatchPush clearBadge];

src/BatchPush.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,49 @@ export const BatchPush = {
3030
*
3131
*
3232
* No effect on Android.
33+
*
34+
* @deprecated Please use requestNotificationAuthorization to request permission when needed, and requestToken at each app launch
35+
*/
36+
registerForRemoteNotifications: (): void => {
37+
if (Platform.OS === 'ios') {
38+
RNBatch.push_registerForRemoteNotifications();
39+
}
40+
},
41+
42+
/**
43+
* Ask iOS users if they want to accept push notifications.
44+
* Required to be able to push users (or use requestProvisionalNotificationAuthorization).
45+
*
46+
* No effect on Android.
47+
*/
48+
requestNotificationAuthorization: (): void => {
49+
if (Platform.OS === 'ios') {
50+
RNBatch.push_requestNotificationAuthorization();
51+
}
52+
},
53+
54+
/**
55+
* Ask iOS for provisional notifications (no alert to users).
56+
* Required to be able to push users (or use requestNotificationAuthorization).
57+
*
58+
* No effect on Android.
59+
*/
60+
requestProvisionalNotificationAuthorization: (): void => {
61+
if (Platform.OS === 'ios') {
62+
RNBatch.push_requestProvisionalNotificationAuthorization();
63+
}
64+
},
65+
66+
/**
67+
* Synchronizes the user's iOS token with Batch. Should be called at each app launch.
68+
*
69+
* No effect on Android.
3370
*/
34-
registerForRemoteNotifications: (): void =>
35-
RNBatch.push_registerForRemoteNotifications(),
71+
refreshToken: (): void => {
72+
if (Platform.OS === 'ios') {
73+
RNBatch.push_refreshToken();
74+
}
75+
},
3676

3777
/**
3878
* Change the used remote notification types on Android. (Ex: sound, vibrate, alert)

0 commit comments

Comments
 (0)