Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Support GCM for ios option #282

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ This plugin offers support to receive and handle native push notifications with
- [PhoneGap Build support](docs/PHONEGAP_BUILD.md)
- [Push notification payload details](docs/PAYLOAD.md)
- [Contributing](CONTRIBUTING.md)
- [License (MIT)](MIT-LICENSE)
- [License (MIT)](MIT-LICENSE)
4 changes: 3 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Attribute | Type | Default | Description
`ios.badge` | `boolean` | `false` | Optional. If `true` the device sets the badge number on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
`ios.sound` | `boolean` | `false` | Optional. If `true` the device plays a sound on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
`ios.clearBadge` | `boolean` | `false` | Optional. If `true` the badge will be cleared on app startup.
`options.ios.senderID` | `string` | `undefined` (Native) | Maps to the project number in the Google Developer Console. Setting this uses GCM for notifications instead of native
`options.ios.gcmSandbox` | `boolean` | `false` | Whether to use prod or sandbox GCM setting. Defaults to false.

### Example

Expand Down Expand Up @@ -255,4 +257,4 @@ push.finish(function() {
}, function() {
console.log('error');
});
```
```
50 changes: 50 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,56 @@
<header-file src="src/ios/AppDelegate+notification.h" />
<header-file src="src/ios/PushPlugin.h" />

<!-- GCM Provided Frameworks Used -->
<framework src="AddressBook.framework" />
<!-- Note these 2 appear in red in app code project but work-->
<framework src="libsqlite3.tbd" />
<framework src="libz.tbd" />

<!-- GCM Code from Cocoapods -->
<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceID.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDConfig.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDDelegate.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDHeaders.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCloudMessaging/Public/CloudMessaging.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCloudMessaging/Public/GGLContext+CloudMessaging.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/Core.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLConfiguration.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLContext.h" />
<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLErrorCode.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMConfig.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMPubSub.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMReceiverDelegate.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMService.h" />
<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GoogleCloudMessaging.h" />

<source-file target-dir="GCM/Libraries" src="src/ios/Google/Libraries/libGGLCloudMessaging.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/Google/Libraries/libGGLCore.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GGLInstanceID/Libraries/libGGLInstanceIDLib.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleIPhoneUtilities/Libraries/libGIP_Reachability.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleSymbolUtilities/Libraries/libGSDK_Overload.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleNetworkingUtilities/Libraries/libGTMSessionFetcher_core.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleNetworkingUtilities/Libraries/libGTMSessionFetcher_full.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTMStackTrace.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_AddressBook.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_DebugUtils.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_GTMURLBuilder.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_KVO.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSData+zlib.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSDictionary+URLArguments.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSScannerJSON.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSStringHTML.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSStringXML.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_Regex.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_RoundedRectPath.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_StringEncoding.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_SystemVersion.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_UIFont+LineHeight.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_core.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_iPhone.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleCloudMessaging/Libraries/libGcmLib.a" framework="true" />
<source-file target-dir="GCM/Libraries" src="src/ios/GoogleInterchangeUtilities/Libraries/libProtocolBuffers.a" framework="true" />
<!-- End GCM Related Files -->
</platform>

<!-- windows -->
Expand Down
277 changes: 277 additions & 0 deletions src/ios/GGLInstanceID/Headers/Public/GGLInstanceID.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
#if GMP_NO_MODULES
#import <Foundation/Foundation.h>
#else
@import Foundation;
#endif

@class GGLInstanceIDConfig;

/**
* @memberof GGLInstanceID
*
* The key for APNS token to be included in the options dictionary when
* registering for GCM (Google Cloud Messaging). The value should be a
* NSData object that represents the APNS token for the app. This
* key is required to get a GCM token.
*/
FOUNDATION_EXPORT NSString *const kGGLInstanceIDRegisterAPNSOption;

/**
* @memberof GGLInstanceID
*
* The key to specify if the APNS token type is sandbox or production. Set
* to YES if the app was built with Sandbox certificate else NO for production.
* At any point of time InstanceID library will support only one type of token.
*/
FOUNDATION_EXPORT NSString *const kGGLInstanceIDAPNSServerTypeSandboxOption;

/**
* @memberof GGLInstanceID
*
* The scope to be used when fetching/deleting a token for
* GCM (Google Cloud Messaging).
*/
FOUNDATION_EXPORT NSString *const kGGLInstanceIDScopeGCM;

/**
* @related GGLInstanceID
*
* The completion handler invoked when the InstanceID token returns. If
* the call fails we return the appropriate `error code` as described below.
*
* @param token The valid token as returned by InstanceID backend.
*
* @param error The error describing why generating a new token
* failed. See the error codes below for a more detailed
* description.
*/
typedef void(^GGLInstanceIDTokenHandler)(NSString *token, NSError *error);


/**
* @related GGLInstanceID
*
* The completion handler invoked when the InstanceID `deleteToken` returns. If
* the call fails we return the appropriate `error code` as described below
*
* @param error The error describing why deleting the token failed.
* See the error codes below for a more detailed description.
*/
typedef void(^GGLInstanceIDDeleteTokenHandler)(NSError *error);

/**
* @related GGLInstanceID
*
* The completion handler invoked when the app identity is created. If the
* identity wasn't created for some reason we return the appropriate error code.
*
* @param identity A valid identity for the app instance, nil if there was an error
* while creating an identity.
* @param error The error if fetching the identity fails else nil.
*/
typedef void(^GGLInstanceIDHandler)(NSString *identity, NSError *error);

/**
* @related GGLInstanceID
*
* The completion handler invoked when the app identity and all the tokens associated
* with it are deleted. Returns a valid error object in case of failure else nil.
*
* @param error The error if deleting the identity and all the tokens associated with
* it fails else nil.
*/
typedef void(^GGLInstanceIDDeleteHandler)(NSError *error);

/**
* @enum GGLInstanceIDOperationErrorCode
* Description of error codes
*/
typedef NS_ENUM(NSUInteger, GGLInstanceIDOperationErrorCode) {
// Http related errors.

/// InvalidRequest -- Some parameters of the request were invalid.
kGGLInstanceIDOperationErrorCodeInvalidRequest = 0,

/// Auth Error -- GCM couldn't validate request from this client.
kGGLInstanceIDOperationErrorCodeAuthentication = 1,

/// NoAccess -- InstanceID service cannot be accessed.
kGGLInstanceIDOperationErrorCodeNoAccess = 2,

/// Timeout -- Request to InstanceID backend timed out.
kGGLInstanceIDOperationErrorCodeTimeout = 3,


/// Network -- No network available to reach the servers.
kGGLInstanceIDOperationErrorCodeNetwork = 4,

/// OperationInProgress -- Another similar operation in progress,
/// bailing this one.
kGGLInstanceIDOperationErrorCodeOperationInProgress = 5,

/// Unknown error.
kGGLInstanceIDOperationErrorCodeUnknown = 7,

// InstanceID specific errors

/*
* Generic errors.
*/

// Device seems to be missing a valid deviceID. Cannot
// authenticate device requests.
kGGLInstanceIDOperationErrorCodeMissingDeviceID = 501,

/**
* Token specific errors.
*/

/// GCM token request is missing APNS token.
kGGLInstanceIDOperationErrorCodeMissingAPNSToken = 1001,

/// GCM token request is missing server type.
kGGLInstanceIDOperationErrorCodeMissingAPNSServerType = 1002,

/// Token request has invalid authorizedEntity.
kGGLInstanceIDOperationErrorCodeInvalidAuthorizedEntity = 1003,

/// Token request has invalid scope.
kGGLInstanceIDOperationErrorCodeInvalidScope = 1004,

/// Should call `startWithConfig:` before requesting token.
kGGLInstanceIDOperationErrorCodeInvalidStart = 1005,

/// KeyPair access error.
kGGLInstanceIDOperationErrorCodeInvalidKeyPair = 1006,

/**
* Identity specific errors.
*/

/// Missing KeyPair.
kGGLInstanceIDOperationErrorCodeMissingKeyPair = 2001,
};

/**
* Instance ID provides a unique identifier for each app instance and a mechanism
* to authenticate and authorize actions (for example, sending a GCM message).
*
* Instance ID is long lived but, may be reset if the device is not used for
* a long time or the Instance ID service detects a problem.
* If Instance ID is reset, the app will be notified with a callback to
* [GGLInstanceIDDelegate onTokenRefresh]
*
* If the Instance ID has become invalid, the app can request a new one and
* send it to the app server.
* To prove ownership of Instance ID and to allow servers to access data or
* services associated with the app, call
* `[GGLInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
*/
@interface GGLInstanceID : NSObject

/**
* GGLInstanceID.
*
* @return A shared instance of GGLInstanceID.
*/
+ (instancetype)sharedInstance;

/**
* Start `GGLInstanceID` with the specified config.
*
* @see GGLInstanceIDConfig
*
* @param config The `GGLInstanceIDConfig` used to build the service.
*/
- (void)startWithConfig:(GGLInstanceIDConfig *)config;

/**
* Stop any network requests started by the client and release any handlers
* associated with it.
*/
- (void)stopAllRequests;

#pragma mark - Tokens

/**
* Returns a token that authorizes an Entity (example: cloud service) to perform
* an action on behalf of the application identified by Instance ID.
*
* This is similar to an OAuth2 token except, it applies to the
* application instance instead of a user.
*
* This is an asynchronous call. If the token fetching fails for some reason
* we invoke the completion callback with nil `token` and the appropriate
* error.
*
* Note, you can only have one `token` or `deleteToken` call for a given
* authorizedEntity and scope at any point of time. Making another such call with the
* same authorizedEntity and scope before the last one finishes will result in an
* error with code `OperationInProgress`.
*
* @see GGLInstanceID deleteTokenWithAuthorizedEntity:scope:handler:
*
* @param authorizedEntity Entity authorized by the token.
* @param scope Action authorized for authorizedEntity.
* @param options The extra options to be sent with your token request. The
* value for the `apns_token` should be the NSData object
* passed to UIApplication's
* `didRegisterForRemoteNotificationsWithDeviceToken` method.
* All other keys and values in the options dict need to be
* instances of NSString or else they will be discarded. Bundle
* keys starting with 'GCM.' and 'GOOGLE.' are reserved.
* @param handler The callback handler which is invoked when the token is
* successfully fetched. In case of success a valid `token` and
* `nil` error are returned. In case of any error the `token`
* is nil and a valid `error` is returned. The valid error
* codes have been documented above.
*/
- (void)tokenWithAuthorizedEntity:(NSString *)authorizedEntity
scope:(NSString *)scope
options:(NSDictionary *)options
handler:(GGLInstanceIDTokenHandler)handler;

/**
* Revokes access to a scope (action) for an entity previously
* authorized by `[GGLInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
*
* This is an asynchronous call. Call this on the main thread since InstanceID lib
* is not thread safe. In case token deletion fails for some reason we invoke the
* `handler` callback passed in with the appropriate error code.
*
* Note, you can only have one `token` or `deleteToken` call for a given
* authorizedEntity and scope at a point of time. Making another such call with the
* same authorizedEntity and scope before the last one finishes will result in an error
* with code `OperationInProgress`.
*
* @param authorizedEntity Entity that must no longer have access.
* @param scope Action that entity is no longer authorized to perform.
* @param handler The handler that is invoked once the unsubscribe call ends.
* In case of error an appropriate error object is returned
* else error is nil.
*/
- (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
scope:(NSString *)scope
handler:(GGLInstanceIDDeleteTokenHandler)handler;

#pragma mark - Identity

/**
* Asynchronously fetch a stable identifier that uniquely identifies the app
* instance. If the identifier has been revoked or has expired, this method will
* return a new identifier.
*
*
* @param handler The handler to invoke once the identifier has been fetched.
* In case of error an appropriate error object is returned else
* a valid identifier is returned and a valid identifier for the
* application instance.
*/
- (void)getIDWithHandler:(GGLInstanceIDHandler)handler;

/**
* Resets Instance ID and revokes all tokens.
*/
- (void)deleteIDWithHandler:(GGLInstanceIDDeleteHandler)handler;

@end
32 changes: 32 additions & 0 deletions src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@protocol GGLInstanceIDDelegate;

typedef NS_ENUM(int8_t, GGLInstanceIDLogLevel) {
kGGLInstanceIDLogLevelDebug,
kGGLInstanceIDLogLevelInfo,
kGGLInstanceIDLogLevelError,
kGGLInstanceIDLogLevelAssert,
};

/**
* The config used to configure different options in GGLInstanceID library.
*/
@interface GGLInstanceIDConfig : NSObject

/**
* Set the GGLInstanceIDDelegate to receive callbacks.
*
* @see GGLInstanceIDDelegate
*/
@property(nonatomic, readwrite, weak) id<GGLInstanceIDDelegate> delegate;

// the log level for the GGLInstanceID library.
@property(nonatomic, readwrite, assign) GGLInstanceIDLogLevel logLevel;

/**
* Initialize a default config with logLevel set to `kGGLInstanceIDLogLevelError`.
*
* @return A default config for GGLInstanceID.
*/
+ (instancetype)defaultConfig;

@end
Loading