Appcenter-sdk-react-native: Feature Request: Add API to enable/disable push notifications for React Native iOS/Android

Created on 6 Nov 2018  路  5Comments  路  Source: microsoft/appcenter-sdk-react-native

Hi guys,
We have an option in our app to disable the push notifications (using 1.8.0 version of the packages), but the await Push.setEnabled() is not working after enable/disable it several times, check the code:

import { 
  NativeModules,
  Platform
} from 'react-native'
import axios from 'axios'
import Push from 'appcenter-push'
import AppCenter from 'appcenter'
import Permissions from 'react-native-permissions'
import { config } from '../config'

const _registerPush = async (enable) => {
  if (enable) {
    const hasPermissions = await _validatePermissions()
    if (hasPermissions) {
      await Push.setEnabled(true)
    } else {
      throw new Error('Permissions')
    } 
  }
  else {
    const installId = await AppCenter.getInstallId()
    await axios.delete(`${config.appCenterApi}/apps/${config.ownerName}/${config.appName}/push/devices/${installId}`)
    await Push.setEnabled(false)
  }
}

const _validatePermissions = async () => {
  if (Platform.OS === 'ios') {
    const response = await Permissions.request('notification')
    if (NativeModules.RegisterPushNotifications) {
      await NativeModules.RegisterPushNotifications.register()
    }
    return response === 'authorized'
  }
  return true
}

Any help is really appreciated,
Nicholls

feature request

Most helpful comment

I'm reopening this so we have a record that this is a feature people want. We won't have an ETA but we'll leave it open so we can prioritize it when we have time.

All 5 comments

On the other hand, the endpoint https://openapi.appcenter.ms/#/push/Push_DeleteInstallId is returning the following error:

{
    "error": {
        "code": 400,
        "message": "export ID is not valid"
    }
}

Let me know and thanks in advance

Including the response I've given from App Center's Intercom support center:

The only way that you can prevent notifications from being sent on iOS is to disable them in the iOS notification center. It's possible to disable all notifications for all apps at a device level via platform APIs, but the APIs in AppCenter will not be able to disable them individually. It's not possible to delete APNS push tokens manually; they will only be deleted after an uninstall, OS upgrade, device wipe, or similar. Unfortunately Apple doesn't provide APIs to do this programmatically, so it's a limitation of our SDK.

I'm reopening this so we have a record that this is a feature people want. We won't have an ETA but we'll leave it open so we can prioritize it when we have time.

@jwallra thanks Jacob for the help! 馃憤

Hi guys, this is our native code to enable push notifications manually from iOS:
RegisterPushNotifications.h

#import <React/RCTBridgeModule.h>

@interface RegisterPushNotifications : NSObject <RCTBridgeModule>
@end

RegisterPushNotifications.m

#import "RegisterPushNotifications.h"

#import <AppCenterReactNativePush/AppCenterReactNativePush.h>
#import <AppCenterReactNativeCrashes/AppCenterReactNativeCrashes.h>
#import <AppCenterReactNativeAnalytics/AppCenterReactNativeAnalytics.h>
#import <AppCenterReactNative/AppCenterReactNative.h>

@implementation RegisterPushNotifications

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(register)
{
  [AppCenterReactNativePush register];
}

@end

Thanks, Nicholls

Was this page helpful?
0 / 5 - 0 ratings