mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Resolved Issue #451: Improve the Network Stack
Resolved Issue #473: Add support for Notification Observers (iOS) and Broadcast Receivers (Android)
This commit is contained in:
@@ -209,10 +209,65 @@ export class AndroidApplication extends observable.Observable implements dts.And
|
||||
|
||||
this._eventsToken = initEvents();
|
||||
this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken);
|
||||
this.context = this.nativeApp.getApplicationContext();
|
||||
this._registerPendingReceivers();
|
||||
}
|
||||
|
||||
private _registeredReceivers = {};
|
||||
private _pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>();
|
||||
private _registerPendingReceivers() {
|
||||
if (this._pendingReceiverRegistrations) {
|
||||
var i = 0;
|
||||
var length = this._pendingReceiverRegistrations.length;
|
||||
for (; i < length; i++) {
|
||||
var registerFunc = this._pendingReceiverRegistrations[i];
|
||||
registerFunc(this.context);
|
||||
}
|
||||
this._pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>();
|
||||
}
|
||||
}
|
||||
|
||||
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||
var that = this;
|
||||
var registerFunc = function (context: android.content.Context) {
|
||||
var receiver = new BroadcastReceiver(onReceiveCallback);
|
||||
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
|
||||
that._registeredReceivers[intentFilter] = receiver;
|
||||
}
|
||||
|
||||
if (this.context) {
|
||||
registerFunc(this.context);
|
||||
}
|
||||
else {
|
||||
this._pendingReceiverRegistrations.push(registerFunc);
|
||||
}
|
||||
}
|
||||
|
||||
public unregisterBroadcastReceiver(intentFilter: string) {
|
||||
var receiver = this._registeredReceivers[intentFilter];
|
||||
if (receiver) {
|
||||
this.context.unregisterReceiver(receiver);
|
||||
this._registeredReceivers[intentFilter] = undefined;
|
||||
delete this._registeredReceivers[intentFilter];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BroadcastReceiver extends android.content.BroadcastReceiver {
|
||||
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
|
||||
|
||||
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||
super();
|
||||
this._onReceiveCallback = onReceiveCallback;
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public onReceive(context: android.content.Context, intent: android.content.Intent) {
|
||||
if (this._onReceiveCallback) {
|
||||
this._onReceiveCallback(context, intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
global.__onUncaughtError = function (error: Error) {
|
||||
if (!types.isFunction(exports.onUncaughtError)) {
|
||||
return;
|
||||
|
||||
31
application/application.d.ts
vendored
31
application/application.d.ts
vendored
@@ -440,6 +440,21 @@ declare module "application" {
|
||||
* String value used when hooking to activityBackPressed event.
|
||||
*/
|
||||
public static activityBackPressedEvent: string;
|
||||
|
||||
/**
|
||||
* Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread.
|
||||
* For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29'
|
||||
* @param intentFilter A string containing the intent filter.
|
||||
* @param onReceiveCallback A callback function that will be called each time the receiver receives a broadcast.
|
||||
*/
|
||||
registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void): void;
|
||||
|
||||
/**
|
||||
* Unregister a previously registered BroadcastReceiver.
|
||||
* For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)'
|
||||
* @param intentFilter A string containing the intent filter with which the receiver was originally registered.
|
||||
*/
|
||||
unregisterBroadcastReceiver(intentFilter: string): void;
|
||||
}
|
||||
|
||||
/* tslint:disable */
|
||||
@@ -457,5 +472,21 @@ declare module "application" {
|
||||
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html) object instance provided to the init of the module.
|
||||
*/
|
||||
nativeApp: UIApplication;
|
||||
|
||||
/**
|
||||
* Adds an observer to the default notification center for the specified notification.
|
||||
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
||||
* @param notificationName A string containing the name of the notification.
|
||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||
*/
|
||||
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): void;
|
||||
|
||||
/**
|
||||
* Removes the observer for the specified notification from the default notification center.
|
||||
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
||||
* @param notificationName A string containing the name of the notification.
|
||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||
*/
|
||||
removeNotificationObserver(notificationName: string): void;
|
||||
}
|
||||
}
|
||||
@@ -121,11 +121,33 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationReceiver extends NSObject {
|
||||
private _onReceiveCallback: (notification: NSNotification) => void;
|
||||
|
||||
static new(): NotificationReceiver {
|
||||
return <NotificationReceiver>super.new();
|
||||
}
|
||||
|
||||
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationReceiver {
|
||||
this._onReceiveCallback = onReceiveCallback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public onReceive(notification: NSNotification): void {
|
||||
this._onReceiveCallback(notification);
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"onReceive": { returns: interop.types.void, params: [NSNotification] }
|
||||
};
|
||||
}
|
||||
|
||||
class IOSApplication implements definition.iOSApplication {
|
||||
|
||||
public nativeApp: any;
|
||||
public rootController: any;
|
||||
private _tnsAppdelegate: TNSAppDelegate;
|
||||
private _registeredObservers = {};
|
||||
|
||||
constructor() {
|
||||
// TODO: in iOS there is the singleton instance, while in Android such does not exist hence we pass it as argument
|
||||
@@ -135,6 +157,19 @@ class IOSApplication implements definition.iOSApplication {
|
||||
public init() {
|
||||
this._tnsAppdelegate = new TNSAppDelegate();
|
||||
}
|
||||
|
||||
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void) {
|
||||
var observer = NotificationReceiver.new().initWithCallback(onReceiveCallback);
|
||||
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
||||
this._registeredObservers[notificationName] = observer;
|
||||
}
|
||||
|
||||
public removeNotificationObserver(notificationName: string) {
|
||||
var observer = this._registeredObservers[notificationName];
|
||||
if (observer) {
|
||||
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: If we have nested require(application) calls we may enter unfinished module state, which will create two delegates, resulting in an exception
|
||||
|
||||
Reference in New Issue
Block a user