/
FCMIntentService.java
151 lines (127 loc) · 5.54 KB
/
FCMIntentService.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
/*------------------------------------------------------------------------
* (The MIT License)
*
* Copyright (c) 2008-2012 Rhomobile, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* http://rhomobile.com
*------------------------------------------------------------------------*/
package com.rhomobile.rhodes.fcm;
import android.content.Context;
import android.content.Intent;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.rhomobile.rhodes.util.ContextFactory;
import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.PushContract;
import com.rhomobile.rhodes.R;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import android.os.Handler;
public class FCMIntentService extends FirebaseMessagingService {
private static final String TAG = FCMIntentService.class.getSimpleName();
public static final FCMListener listener = FCMListener.getInstance();
static private String lastHandledIntent = null;
private static FirebaseMessagingService savedService = null;
private static Map<String, Intent> savedIntents = new HashMap<String, Intent>();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
lastHandledIntent = null;
savedIntents.remove(remoteMessage.getMessageId());
Logger.W(TAG, "FCM: onMessageReceived()");
Map<String, String> params = new HashMap<String, String>();
params.put("id", remoteMessage.getMessageId());
params.put("from", remoteMessage.getFrom());
params.put("to", remoteMessage.getTo());
params.put("body", remoteMessage.getNotification().getBody());
params.put("title", remoteMessage.getNotification().getTitle());
params.put("tag", remoteMessage.getNotification().getTag());
JSONObject jsonObject = new JSONObject(params);
try {
jsonObject.put("data", new JSONObject(remoteMessage.getData()));
} catch (JSONException e) {
e.printStackTrace();
}
Logger.W(TAG, "FCM: push message: " + remoteMessage.getNotification().getBody());
Logger.W(TAG, "FCM: push message in JSON: " + jsonObject.toString());
PushContract.handleMessage(ContextFactory.getContext(), jsonObject.toString(), FCMFacade.FCM_PUSH_CLIENT);
}
@Override
public void onDeletedMessages() {
Logger.W(TAG, "FCM: onDeletedMessages()");
}
@Override
synchronized public void handleIntent(Intent intent) {
Logger.W(TAG, "FCM: onHandleIntent()");
savedService = this;
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
//Logger.W(TAG, "Key: " + key + " Value: " + value);
if (key.equals("google.message_id")){
savedIntents.put((String) value, intent);
lastHandledIntent = (String) value;
Logger.W(TAG, "FCM: onHandleIntent() : message id captured");
}
}
}
super.handleIntent(intent);
}
public static void tryToHandleIntent(String value){
try{
if (savedService != null){
Logger.W(TAG, "FCM: tryToHandleIntent() - trying to handle intent");
if (savedIntents.containsKey(value)){
savedService.handleIntent(savedIntents.get(value));
Logger.W(TAG, "FCM: tryToHandleIntent() - intent handled");
}
}
}catch(Exception e){
Logger.W(TAG, "FCM: tryToHandleIntent() - can't handle intent");
}
}
static public void resume()
{
if (lastHandledIntent != null){
Timer timerObj = new Timer();
TimerTask timerTaskObj = new TimerTask() {
public void run() {
if (lastHandledIntent != null){
tryToHandleIntent(lastHandledIntent);
}
}
};
timerObj.schedule(timerTaskObj, 1000);
}
}
}