Skip to content
Permalink
Browse files

My personal approach on Firebase Notifications for iOS and Android

I have been woking like this for months now and I have no problem whatsoever. If iOS the app uses `FuseJS/Push` and if Android it uses our costume wrapper. After that, this is what I do:

1. Added Firebase Database because that configures the app
2. After the registration succeeds, get the token id
3. (Optional) Save this token in Firebase Database
4. (Optional) Use Firebase Cloud Functions to send the notifications
  • Loading branch information...
LuisRodriguezLD committed May 23, 2018
1 parent 6a9aa6f commit ce921761d1bf7f7551703aa54207e795fa863695
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DUMMY_FILE</key>
<string>REPLACE THIS FILE WITH YOUR google-services.json FILE</string>
</dict>
</plist>
@@ -1,12 +1,26 @@
var Push = require("Firebase/Notifications");
var Environment = require("FuseJS/Environment");
var PushiOS = require("FuseJS/Push");
var PushAndroid = require("Firebase/Notifications");
var Observable = require("FuseJS/Observable");
var Nots = require("TokenModule");

var Push;
if(Environment.ios){
Push = PushiOS;
} else {
Push = PushAndroid;
}

var status = Observable("-");
var message = Observable("-no message yet-");

Push.onRegistrationSucceeded = function(regID) {
console.log ("Reg Succeeded: " + regID);
status.value = "onRegistrationSucceeded: " + regID;
Nots.GetFBToken()
.then(function(response) {
console.log("Firebase Token: " + response)
status.value = "Success! Check your console";
});
};

Push.onRegistrationFailed = function(reason) {
@@ -19,17 +33,16 @@ Push.onReceivedMessage = function(payload, fromNotificationBar) {
message.value = payload;
};

var clearBadgeNumber = function() {
Push.clearBadgeNumber();
}

var clearAllNotifications = function() {
Push.clearAllNotifications();
function getToken(){
Nots.GetFBToken()
.then(function(response) {
console.log("Firebase Token: " + response)
status.value = "Success! Check your console";
});
}

module.exports = {
clearBadgeNumber: clearBadgeNumber,
clearAllNotifications: clearAllNotifications,
message: message,
status: status
status: status,
getToken
};
@@ -5,16 +5,13 @@
<StatusBarBackground/>
<Fuse.iOS.StatusBarConfig Style="Light"/>
</StackPanel>
<StackPanel Width="92%" Height="460" Padding="20">
<DockPanel Width="92%" Height="88%" Padding="10,33">
<Rectangle Layer="Background" CornerRadius="10" Color="#fff"/>
<Text Value="Notifications Test" Alignment="Center" FontSize="35" TextColor="#2586EC"/>

<Text Value="Nothing"/>
<Text Value="{status}"/>
<Button Clicked="{clearBadgeNumber}" Text="clearBadgeNumber"/>
<Button Clicked="{clearAllNotifications}" Text="clearAllNotifications"/>
<Text Value="{message}"/>
</StackPanel>
<Text TextAlignment="Center" Dock="Top" Value="Notifications Test" Alignment="Center" FontSize="35" TextColor="#2586EC"/>
<Text TextAlignment="Center" Alignment="Center" TextWrapping="Wrap" Value="{message}"/>
<Text TextAlignment="Center" Dock="Bottom" Value="{status}"/>
<Button Dock="Bottom" Text="getToken" Clicked="{getToken}"/>
</DockPanel>
<BottomBarBackground Dock="Bottom" />
</DockPanel>
</App>
@@ -1,25 +1,34 @@
{
"Packages": [
"Fuse",
"FuseJS",
"Fuse.BasicTheme"
],
"Projects": [
"../src/Firebase.Notifications.Android/Firebase.Notifications.Android.unoproj",
],
"Android": {
"NotificationIcon": {
"Packages": [
"Fuse",
"FuseJS",
"Fuse.PushNotifications",
"Fuse.BasicTheme"
],
"Projects": [
"../src/Firebase/Firebase.unoproj",
"../src/Firebase.Database/Firebase.Database.unoproj",
"../src/Firebase.Notifications/Firebase.Notifications.unoproj",
],
"Android": {
"NotificationIcon": {
"Color": "FF00FF",
"LDPI": "Foo.png",
"MDPI": "Foo.png",
"HDPI": "Foo.png",
"XHDPI": "Foo.png",
"XXHDPI": "Foo.png",
"XXXHDPI": "Foo.png"
"XXXHDPI": "Foo.png"
},
},
},
"Includes": [
"*",
"assets/thing.png:Bundle"
]
"iOS": {
"SystemCapabilities": {
"Push": true
}
},
"Includes": [
"*",
"assets/thing.png:Bundle",
"GoogleService-Info.plist:ObjCSource:iOS"
]
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
File renamed without changes.
@@ -25,7 +25,6 @@ namespace Firebase.Notifications
extern(Android)
static NotificationService()
{
Firebase.Core.Init();
AndroidImpl.ReceivedNotification += OnReceived;
AndroidImpl.RegistrationFailed += OnRegistrationFailed;
AndroidImpl.RegistrationSucceeded += OnRegistrationSucceeded;
File renamed without changes.
@@ -22,13 +22,11 @@
"Includes": [
"Common.uno:Source",
"JS.uno:Source",
"Token.uno:Source",
"Android/Impl.uno:Source",
"iOSImpl.uno:Source",
"Android/Impl.cpp.uxl:Extensions",
"Android/PushNotificationReceiver.java:Java:android",
"Android/PushNotificationIDService.java:Java:android",
"iOSFirebaseNotificationCallbacks.h:ObjCHeader:iOS",
"iOSFirebaseNotificationCallbacks.mm:ObjCSource:iOS",
"Android/Assets/DefaultIcon.png:File"
]
}
@@ -26,18 +26,14 @@ namespace Firebase.Notifications
public sealed class NotificationModule : NativeEventEmitterModule
{
static readonly NotificationModule _instance;
readonly iOSImpl _iOSImpl;
static NativeEvent _onRegistrationSucceedediOS;

public NotificationModule()
: base(true,
"receivedMessage",
"registrationSucceeded")
{
if (_instance != null) return;
Resource.SetGlobalKey(_instance = this, "Firebase/Notifications");

_iOSImpl = new iOSImpl();
Uno.UX.Resource.SetGlobalKey(_instance = this, "Firebase/Notifications");

// Old-style events for backwards compatibility
var onReceivedMessage = new NativeEvent("onReceivedMessage");
@@ -59,9 +55,6 @@ namespace Firebase.Notifications
AddMember(onRegistrationFailed);
AddMember(new NativeFunction("clearBadgeNumber", ClearBadgeNumber));
AddMember(new NativeFunction("clearAllNotifications", ClearAllNotifications));
AddMember(new NativeFunction("getFCMToken", GetFCMToken));
_onRegistrationSucceedediOS = new NativeEvent("onRegistrationSucceedediOS");
AddMember(_onRegistrationSucceedediOS);

Firebase.Notifications.NotificationService.ReceivedNotification += OnReceivedNotification;
Firebase.Notifications.NotificationService.RegistrationSucceeded += OnRegistrationSucceeded;
@@ -90,11 +83,6 @@ namespace Firebase.Notifications
Emit("registrationSucceeded", message);
}

static void OnRegistrationSucceedediOS(string message) {
//_onRegistrationSucceedediOS.RaiseAsync(message);
// App is getting crash sometimes at this function and now we are getting FCM token via GetFCMToken(), so we can put it in comment
}

/**
@scriptevent error
@param message A backend specific reason for the failure.
@@ -129,14 +117,5 @@ namespace Firebase.Notifications
Firebase.Notifications.NotificationService.ClearAllNotifications();
return null;
}

public object GetFCMToken(Context context, object[] args)
{
var token = Firebase.Notifications.NotificationService.GetFCMToken();
if (token != null) {
Emit("registrationSucceeded", token);
}
return null;
}
}
}
@@ -0,0 +1,56 @@
using Uno;
using Uno.UX;
using Uno.Threading;
using Uno.Text;
using Uno.Platform;
using Uno.Compiler.ExportTargetInterop;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;

[UXGlobalModule]
public class TokenModule : NativeModule
{
static readonly TokenModule _instance;
public TokenModule()
{
if (_instance != null) return;

_instance = this;
Resource.SetGlobalKey(_instance, "TokenModule");
AddMember(new NativePromise<string, string>("GetFBToken", GetFBToken, null));
}

static string GetFBToken(object[] args)
{
InitForeign.Init();
return InitForeign.Token;
}
}

class InitForeign
{
public static string Token = "";

[Require("Source.Include", "Firebase/Firebase.h")]
[Foreign(Language.ObjC)]
public static extern(iOS) void Init()
@{
[NSThread sleepForTimeInterval:1];
NSString *fcmToken = [[FIRInstanceID instanceID] token];
@{Token:Set(fcmToken)};
@}

[Foreign(Language.Java)]
public static extern(Android) void Init()
@{
String id = com.google.firebase.iid.FirebaseInstanceId.getInstance().getToken();
@{Token:Set(id)};
@}

public static extern(!mobile) void Init()
{
debug_log("Not Implemented for Desktop");
}
}

0 comments on commit ce92176

Please sign in to comment.
You can’t perform that action at this time.