-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirebaseIntegration.cs
88 lines (71 loc) · 3.82 KB
/
FirebaseIntegration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_ANDROID
namespace HelpshiftExample
{
public class FirebaseIntegration
{
public static void initFirebase()
{
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
// Set a flag here to indicate whether Firebase is ready to use by your app.
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
else
{
Debug.LogError(System.String.Format(
"Could not resolve all Firebase dependencies: {0}", dependencyStatus));
// Firebase Unity SDK is not safe to use here.
}
});
}
public static void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
Debug.Log("Received Registration Token: " + token.Token);
Helpshift.HelpshiftSdk.GetInstance().RegisterPushToken(token.Token);
}
public static void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
Debug.Log("Received a new message from: " + e.Message.From);
IDictionary<string, string> pushData = e.Message.Data;
/// Check if the notification origin is Helpshift
if (pushData.ContainsKey("origin") && pushData["origin"].Equals("helpshift"))
{
Dictionary<string, object> hsPushData = new Dictionary<string, object>();
Debug.Log("Received a new message for Helpshift");
foreach (string key in pushData.Keys)
{
hsPushData.Add(key, pushData[key]);
}
// Handle the notification with Helpshift SDK.
Helpshift.HelpshiftSdk.GetInstance().HandlePushNotification(hsPushData);
return;
}
/// Handle notification from other sources
/// For example, lets say you send notification for Helpshift Outbound support
/// and embed the outbound link in "helpshift_proactive_link" key
if (pushData.ContainsKey("helpshift_proactive_link"))
{
// Recevied notification from customer's push integration.
// Extract proactive outbound link for Helpshift Outbound Support
string outboundLink = pushData["helpshift_proactive_link"];
// Embed this outboundLink in a notification Intent and post notification on device.
// When user clicks the notification then extract this outboundLink from the notification intent extra data and call
// Helpshift.HelpshiftSdk.GetInstance().HandleProactiveLink(outboundLink);
// Below example demonstrates the above logic.
AndroidJavaClass notificationUtil = new AndroidJavaClass("com.helpshift.unity.sdkx.helpshiftexamplenativehandler.HelpshiftNotificationUtil");
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
notificationUtil.CallStatic("showNotification", new object[] { currentActivity, "Helpshift Example: Outbound Support Notification", outboundLink});
return;
}
}
}
}
#endif