-
-
Save jogboms/dec06ba426a42b14ebe1a0dba1611382 to your computer and use it in GitHub Desktop.
Android push notifications with EddyVerbruggen/nativescript-plugin-firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AndroidActivityCallbacks, setActivityCallbacks } from "ui/frame"; | |
import { ReplaySubject } from "rxjs/ReplaySubject"; | |
import { Observable } from "rxjs/Observable"; | |
const _intentSubject = new ReplaySubject<android.content.Intent>(); | |
export const OnIntent: Observable<any> = _intentSubject.asObservable(); // apply transformation as needed to abstract from platform | |
@JavaProxy("org.example.App.MainActivity") | |
export class Activity extends android.app.Activity { | |
private _callbacks: AndroidActivityCallbacks; | |
protected onCreate(savedInstanceState: android.os.Bundle): void { | |
if (!this._callbacks) { | |
setActivityCallbacks(this); | |
} | |
this._callbacks.onCreate(this, savedInstanceState, super.onCreate); | |
const creationIntent = this.getIntent(); | |
_intentSubject.next(creationIntent); | |
} | |
protected onNewIntent(intent: android.content.Intent): void { | |
super.onNewIntent(intent); | |
this.setIntent(intent); | |
_intentSubject.next(intent); | |
} | |
protected onSaveInstanceState(outState: android.os.Bundle): void { | |
this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState); | |
} | |
protected onStart(): void { | |
this._callbacks.onStart(this, super.onStart); | |
} | |
protected onStop(): void { | |
this._callbacks.onStop(this, super.onStop); | |
} | |
protected onDestroy(): void { | |
this._callbacks.onDestroy(this, super.onDestroy); | |
} | |
public onBackPressed(): void { | |
this._callbacks.onBackPressed(this, super.onBackPressed); | |
} | |
public onRequestPermissionsResult(requestCode: number, permissions: Array<String>, grantResults: Array<number>): void { | |
this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/); | |
} | |
protected onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void { | |
this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from "rxjs/Observable"; | |
export declare const OnIntent: Observable<android.content.Intent>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from "rxjs/Observable"; | |
import "rxjs/add/observable/empty"; | |
export const OnIntent = Observable.empty(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="__PACKAGE__" | |
android:versionCode="1" | |
android:versionName="1.0"> | |
<supports-screens | |
android:smallScreens="true" | |
android:normalScreens="true" | |
android:largeScreens="true" | |
android:xlargeScreens="true"/> | |
<uses-sdk | |
android:minSdkVersion="17" | |
android:targetSdkVersion="__APILEVEL__"/> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:name="com.tns.NativeScriptApplication" | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme"> | |
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" | |
android:resource="@drawable/ic_stat_content_notification" /> | |
<activity | |
android:name="org.example.App.MainActivity" | |
android:launchMode="singleTask" | |
android:label="@string/title_activity_kimera" | |
android:configChanges="keyboardHidden|orientation|screenSize" | |
android:theme="@style/LaunchScreenTheme"> | |
<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" /> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
<intent-filter> | |
<action android:name="android.intent.action.VIEW" /> | |
<category android:name="android.intent.category.DEFAULT" /> | |
<category android:name="android.intent.category.BROWSABLE" /> | |
<data android:scheme="https" /> | |
<data android:host="app.org" /> | |
</intent-filter> | |
</activity> | |
<activity android:name="com.tns.ErrorReportActivity"/> | |
<service android:name="org.nativescript.plugins.firebase.MyFirebaseInstanceIDService"> | |
<intent-filter> | |
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> | |
</intent-filter> | |
</service> | |
<service android:name="org.nativescript.plugins.firebase.MyFirebaseMessagingService"> | |
<intent-filter> | |
<action android:name="com.google.firebase.MESSAGING_EVENT"/> | |
</intent-filter> | |
</service> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, NgZone } from "@angular/core"; | |
import { Observable } from "rxjs/Observable"; | |
import { RouterExtensions } from "nativescript-angular"; | |
import { onIntent } from "./activity"; | |
@Component({selector: "app", templateUrl: "app.component.html"}) | |
export class AppComponent implements OnInit { | |
constructor(private _zone: NgZone) {...} | |
ngOnInit() { | |
// subscribe to notification events from both platforms | |
onIntent.subscribe((notification) => this._doSomething()); | |
// if you are doing something with Angular based on the notifications, you should probably run it in NgZone | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment