Skip to content

Commit ae81bba

Browse files
authored
Merge pull request #1184 from OneSignal/fix/implement_ADMMessageHandlerJobBase
Fix/implement adm message handler job base
2 parents 349fdec + 7143c42 commit ae81bba

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

Examples/OneSignalDemo/app/src/main/AndroidManifest.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4+
xmlns:amazon="http://schemas.amazon.com/apk/res/android"
45
package="com.onesignal.sdktest">
56

67
<uses-permission android:name="com.android.vending.BILLING" />
@@ -9,6 +10,16 @@
910
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
1011
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
1112

13+
<permission
14+
android:name="com.onesignal.sdktest.permission.RECEIVE_ADM_MESSAGE"
15+
android:protectionLevel="signature" />
16+
<uses-permission android:name="com.onesignal.sdktest.permission.RECEIVE_ADM_MESSAGE" />
17+
<!-- This permission allows your app access to receive push notifications from ADM. -->
18+
<uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE" />
19+
20+
<!-- ADM uses WAKE_LOCK to keep the processor from sleeping when a message is received. -->
21+
<uses-permission android:name="android.permission.WAKE_LOCK" />
22+
1223
<application
1324
android:name=".application.MainApplication"
1425
android:allowBackup="true"
@@ -19,6 +30,37 @@
1930
android:theme="@style/AppTheme"
2031
tools:ignore="GoogleAppIndexingWarning">
2132

33+
<amazon:enable-feature
34+
android:name="com.amazon.device.messaging"
35+
android:required="false"/>
36+
37+
<!-- You must replace the names in the service and receiver tags
38+
with names that are appropriate to your package. -->
39+
40+
<service
41+
android:name="com.onesignal.ADMMessageHandlerJob"
42+
android:permission="android.permission.BIND_JOB_SERVICE"
43+
android:exported="false" />
44+
45+
<!-- This is needed for devices with older ADM versions -->
46+
<service
47+
android:name="com.onesignal.ADMMessageHandler"
48+
android:exported="false" />
49+
50+
<receiver
51+
android:name="com.onesignal.ADMMessageHandler$Receiver"
52+
android:permission="com.amazon.device.messaging.permission.SEND" >
53+
54+
<!-- To interact with ADM, your app must listen for the following intents. -->
55+
<intent-filter>
56+
<action android:name="com.amazon.device.messaging.intent.REGISTRATION" />
57+
<action android:name="com.amazon.device.messaging.intent.RECEIVE" />
58+
59+
<!-- Replace the name in the category tag with your app's package name. -->
60+
<category android:name="com.onesignal.sdktest" />
61+
</intent-filter>
62+
</receiver>
63+
2264
<meta-data android:name="com.onesignal.NotificationServiceExtension"
2365
android:value="com.onesignal.sdktest.notification.NotificationServiceExtension" />
2466

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Insert Amazon API Key here.
2+
https://documentation.onesignal.com/docs/generate-an-amazon-api-key

OneSignalSDK/onesignal/consumer-proguard-rules.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
# Proguard ends up removing this class even if it is used in AndroidManifest.xml so force keeping it.
5656
-keep public class com.onesignal.ADMMessageHandler {*;}
5757

58+
-keep public class com.onesignal.ADMMessageHandlerJob {*;}
59+
5860
-keep class com.onesignal.JobIntentService$* {*;}
5961

6062
-keep class com.onesignal.OneSignalUnityProxy {*;}
Binary file not shown.
Binary file not shown.

OneSignalSDK/onesignal/src/main/java/com/onesignal/ADMMessageHandler.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.content.Intent;
3232
import android.os.Bundle;
3333

34+
import com.amazon.device.messaging.ADMMessageHandlerJobBase;
3435
import com.amazon.device.messaging.ADMMessageHandlerBase;
3536
import com.amazon.device.messaging.ADMMessageReceiver;
3637

@@ -40,9 +41,24 @@
4041
// when "proguard-android-optimize.txt" is used.
4142
public class ADMMessageHandler extends ADMMessageHandlerBase {
4243

44+
private static final int JOB_ID = 123891;
45+
4346
public static class Receiver extends ADMMessageReceiver {
4447
public Receiver() {
4548
super(ADMMessageHandler.class);
49+
boolean ADMLatestAvailable = false;
50+
try {
51+
Class.forName( "com.amazon.device.messaging.ADMMessageHandlerJobBase" );
52+
ADMLatestAvailable = true ;
53+
}
54+
catch (ClassNotFoundException e)
55+
{
56+
// Handle the exception.
57+
}
58+
if (ADMLatestAvailable) {
59+
registerJobServiceClass(ADMMessageHandlerJob.class, JOB_ID);
60+
}
61+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "ADM latest available: " + ADMLatestAvailable);
4662
}
4763
}
4864

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.onesignal
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import com.amazon.device.messaging.ADMMessageHandlerJobBase
6+
7+
class ADMMessageHandlerJob : ADMMessageHandlerJobBase() {
8+
9+
override fun onMessage(context: Context?, intent: Intent?) {
10+
val bundle = intent?.extras
11+
12+
val processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle)
13+
// TODO: Figure out the correct replacement or usage of completeWakefulIntent method
14+
// FCMBroadcastReceiver.completeWakefulIntent(intent);
15+
16+
if (processedResult.processed()) return
17+
18+
val payload = NotificationBundleProcessor.bundleAsJSONObject(bundle)
19+
val notification = OSNotification(payload)
20+
21+
val notificationJob = OSNotificationGenerationJob(context).apply {
22+
this.jsonPayload = payload
23+
this.context = context
24+
this.notification = notification
25+
}
26+
27+
NotificationBundleProcessor.processJobForDisplay(notificationJob, true)
28+
}
29+
30+
override fun onRegistered(context: Context?, newRegistrationId: String?) {
31+
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "ADM registration ID: $newRegistrationId")
32+
PushRegistratorADM.fireCallback(newRegistrationId)
33+
}
34+
35+
override fun onUnregistered(context: Context?, registrationId: String?) {
36+
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "ADM:onUnregistered: $registrationId")
37+
}
38+
39+
override fun onRegistrationError(context: Context?, error: String?) {
40+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "ADM:onRegistrationError: $error")
41+
if ("INVALID_SENDER" == error) OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Please double check that you have a matching package name (NOTE: Case Sensitive), api_key.txt, and the apk was signed with the same Keystore and Alias.")
42+
43+
PushRegistratorADM.fireCallback(null)
44+
}
45+
}

0 commit comments

Comments
 (0)