-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDDNANotifications.java
221 lines (196 loc) · 7.86 KB
/
DDNANotifications.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (c) 2016 deltaDNA Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.deltadna.android.sdk.notifications;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import com.deltadna.android.sdk.DDNA;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* Helper class for easily registering/un-registering for/from push
* notifications.
*/
@UnityInterOp
public final class DDNANotifications {
private static final String TAG = BuildConfig.LOG_TAG
+ ' '
+ DDNANotifications.class.getSimpleName();
private static final String NAME = "deltadna-sdk-notifications";
private static final Executor EXECUTOR = Executors.newSingleThreadExecutor();
@Nullable
static Class<? extends EventReceiver> receiver = null;
/**
* Register the client for push notifications.
* <p>
* A good time to perform this would be for example when the user enables
* push notifications from the game's settings, or when a previous
* registration attempt fails as notified by an {@link EventReceiver}.
* <p>
* This method registers the Firebase Cloud Messaging used by deltaDNA as
* the main one.
*
* @param context the context
*
* @throws IllegalStateException if the configuration meta-data entries
* are missing from the manifest
*
* @see #register(Context, boolean)
* @see DDNA#setRegistrationId(String)
*/
public static void register(Context context) {
register(context, false);
}
/**
* Register the client for push notifications.
* <p>
* A good time to perform this would be for example when the user enables
* push notifications from the game's settings, or when a previous
* registration attempt fails as notified by an {@link EventReceiver}.
* <p>
* If you have multiple Firebase Cloud Messaging senders in your project
* then you can use the deltaDNA sender either as a main one or as a
* secondary one by setting the {@code secondary} parameter. If you set
* {@code secondary} to {@code true} then the default FCM sender will need
* to have been initialised beforehand.
*
* @param context the context
* @param secondary whether the {@link FirebaseApp} instance used for
* deltaDNA notifications should be registered as a
* secondary (non-main) instance
*
* @throws IllegalStateException if the configuration meta-data entries
* are missing from the manifest
*
* @see DDNA#setRegistrationId(String)
*/
public static void register(final Context context, boolean secondary) {
Log.d(TAG, "Registering for push notifications");
final String applicationId;
final String senderId;
final String projectId;
final String apiKey;
try {
final Bundle metaData = MetaData.get(context);
applicationId = context.getString(
metaData.getInt(MetaData.APPLICATION_ID));
senderId = context.getString(
metaData.getInt(MetaData.SENDER_ID));
projectId = context.getString(
metaData.getInt(MetaData.PROJECT_ID));
apiKey = context.getString(
metaData.getInt(MetaData.FCM_API_KEY));
} catch (final Resources.NotFoundException e) {
throw new IllegalStateException(
String.format(
Locale.US,
"Failed to find configuration meta-data, have %s , %s , %s and %s been defined in the manifest?",
MetaData.APPLICATION_ID,
MetaData.SENDER_ID, MetaData.PROJECT_ID, MetaData.FCM_API_KEY),
e);
}
synchronized (DDNANotifications.class) {
final String name = secondary ? NAME : FirebaseApp.DEFAULT_APP_NAME;
boolean found = false;
for (FirebaseApp app : FirebaseApp.getApps(context)) {
if (app.getName().equals(name)) {
found = true;
}
}
if (!found) {
/*
* Running this the first time will force a token refresh, in
* other use cases the service will be invoked when the token
* will need to be refreshed so there is no need to perform
* a forced refresh at this point.
*/
FirebaseApp.initializeApp(
context,
new FirebaseOptions.Builder()
.setApplicationId(applicationId)
.setGcmSenderId(senderId)
.setProjectId(projectId)
.setApiKey(apiKey)
.build(),
name);
}
EXECUTOR.execute(new Runnable() {
@Override
public void run() {
RegistrationTokenFetcher.fetch(context);
}
});
}
}
/**
* Unregister the client from push notifications.
*
* @see DDNA#clearRegistrationId()
*
* @throws UnsupportedOperationException if called from Unity
*/
public static void unregister() {
if (UnityForwarder.isPresent()) {
throw new UnsupportedOperationException(
"Unity SDK should unregister from its own code");
}
Log.d(TAG, "Unregistering from push notifications");
DDNA.instance().clearRegistrationId();
}
/**
* Allows for a {@link EventReceiver} class to be registered for event
* broadcasts published by the SDK. This method should be used if your
* build is targeting API 26 and higher.
*
* @param receiver the {@link EventReceiver}, may be {@code null} to
* unregister
*/
public static void setReceiver(
@Nullable Class<? extends EventReceiver> receiver) {
synchronized (DDNANotifications.class) {
DDNANotifications.receiver = receiver;
}
}
/**
* Notifies the SDK that a push notification has been opened by the user.
*
* @param payload the payload of the push notification
* @param launch whether the notification launched the app
*/
public static void recordNotificationOpened(
Bundle payload,
boolean launch) {
if (UnityForwarder.isPresent()) {
final Bundle copy = new Bundle(payload);
copy.putBoolean("_ddLaunch", launch);
UnityForwarder.getInstance().forward(
"DeltaDNA.AndroidNotifications",
"DidReceivePushNotification",
Utils.convert(copy));
} else {
DDNA.queueRecordNotificationOpened(launch, payload);
}
}
public static void markUnityLoaded() {
UnityForwarder.getInstance().markLoaded();
}
private DDNANotifications() {}
}