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

Commit

Permalink
Add new methods for requesting iOS permission and deprecate old one
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink committed Nov 24, 2021
1 parent a5b1ac9 commit 137f1a1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ If you want to show foreground notifications, add the [relevant configuration](h
```js
import { BatchPush } from '@bam.tech/react-native-batch';

BatchPush.registerForRemoteNotifications();
// when you want to show the alert asking for permission
BatchPush.requestNotificationAuthorization();

// at each app launch (after opt-in if you're opted out by default)
BatchPush.refreshToken();
```

<hr>
Expand Down
15 changes: 15 additions & 0 deletions ios/RNBatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ - (NSDictionary*) dictionaryWithEventDispatcherPayload:(id<BatchEventDispatcherP
[BatchPush registerForRemoteNotifications];
}

RCT_EXPORT_METHOD(push_requestNotificationAuthorization)
{
[BatchPush requestNotificationAuthorization];
}

RCT_EXPORT_METHOD(push_requestProvisionalNotificationAuthorization)
{
[BatchPush requestProvisionalNotificationAuthorization];
}

RCT_EXPORT_METHOD(push_refreshToken)
{
[BatchPush refreshToken];
}

RCT_EXPORT_METHOD(push_clearBadge)
{
[BatchPush clearBadge];
Expand Down
44 changes: 42 additions & 2 deletions src/BatchPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,49 @@ export const BatchPush = {
*
*
* No effect on Android.
*
* @deprecated Please use requestNotificationAuthorization to request permission when needed, and requestToken at each app launch
*/
registerForRemoteNotifications: (): void => {
if (Platform.OS === 'ios') {
RNBatch.push_registerForRemoteNotifications();
}
},

/**
* Ask iOS users if they want to accept push notifications.
* Required to be able to push users (or use requestProvisionalNotificationAuthorization).
*
* No effect on Android.
*/
requestNotificationAuthorization: (): void => {
if (Platform.OS === 'ios') {
RNBatch.push_requestNotificationAuthorization();
}
},

/**
* Ask iOS for provisional notifications (no alert to users).
* Required to be able to push users (or use requestNotificationAuthorization).
*
* No effect on Android.
*/
requestProvisionalNotificationAuthorization: (): void => {
if (Platform.OS === 'ios') {
RNBatch.push_requestProvisionalNotificationAuthorization();
}
},

/**
* Synchronizes the user's iOS token with Batch. Should be called at each app launch.
*
* No effect on Android.
*/
registerForRemoteNotifications: (): void =>
RNBatch.push_registerForRemoteNotifications(),
refreshToken: (): void => {
if (Platform.OS === 'ios') {
RNBatch.push_refreshToken();
}
},

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

0 comments on commit 137f1a1

Please sign in to comment.