13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
- package org .mifos .mobilewallet .mifospay .data .firebase .api .services ;
18
-
19
- import android .app .NotificationChannel ;
20
- import android .app .NotificationManager ;
21
- import android .app .PendingIntent ;
22
- import android .content .Context ;
23
- import android .content .Intent ;
24
- import android .media .RingtoneManager ;
25
- import android .net .Uri ;
26
- import android .os .Build ;
27
- import androidx .core .app .NotificationCompat ;
28
- import android .util .Log ;
29
-
30
- import com .google .firebase .messaging .FirebaseMessagingService ;
31
- import com .google .firebase .messaging .RemoteMessage ;
32
-
33
- import org .json .JSONException ;
34
- import org .json .JSONObject ;
35
- import org .mifos .mobilewallet .mifospay .R ;
36
- import org .mifos .mobilewallet .mifospay .notification .ui .NotificationActivity ;
37
- import org .mifos .mobilewallet .mifospay .utils .NotificationUtils ;
16
+ package org.mifos.mobilewallet.mifospay.data.firebase.api.services
17
+
18
+ import android.app.NotificationChannel
19
+ import android.app.NotificationManager
20
+ import android.app.PendingIntent
21
+ import android.content.Context
22
+ import android.content.Intent
23
+ import android.media.RingtoneManager
24
+ import android.os.Build
25
+ import android.util.Log
26
+ import androidx.core.app.NotificationCompat
27
+ import com.google.firebase.messaging.FirebaseMessagingService
28
+ import com.google.firebase.messaging.RemoteMessage
29
+ import org.json.JSONException
30
+ import org.json.JSONObject
31
+ import org.mifos.mobilewallet.mifospay.R
32
+ import org.mifos.mobilewallet.mifospay.notification.ui.NotificationActivity
33
+ import org.mifos.mobilewallet.mifospay.utils.NotificationUtils
38
34
39
35
/* *
40
36
* Created by ankur on 20/June/2018
41
37
*/
42
-
43
- public class MifosPayMessagingService extends FirebaseMessagingService {
44
-
45
- private static final String TAG = "MifosPayFCM" ;
46
-
47
-
38
+ class MifosPayMessagingService : FirebaseMessagingService () {
48
39
/* *
49
40
* Called if InstanceID token is updated. This may occur if the security of
50
41
* the previous token had been compromised. Note that this is called when the InstanceID token
51
42
* is initially generated so this is where you would retrieve the token.
52
43
*/
53
- @ Override
54
- public void onNewToken (String token ) {
55
- super .onNewToken (token );
56
- Log .d (TAG , "Refreshed token: " + token );
44
+ override fun onNewToken (token : String ) {
45
+ super .onNewToken(token)
46
+ Log .d(TAG , " Refreshed token: $token " )
57
47
// If you want to send messages to this application instance or
58
48
// manage this apps subscriptions on the server side, send the
59
49
// Instance ID token to your app server.
@@ -65,8 +55,7 @@ public void onNewToken(String token) {
65
55
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
66
56
*/
67
57
// [START receive_message]
68
- @ Override
69
- public void onMessageReceived (RemoteMessage remoteMessage ) {
58
+ override fun onMessageReceived (remoteMessage : RemoteMessage ) {
70
59
// [START_EXCLUDE]
71
60
// There are two types of messages data messages and notification messages. Data messages
72
61
// are handled
@@ -84,18 +73,17 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
84
73
// [END_EXCLUDE]
85
74
86
75
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
87
- Log .d (TAG , "From: " + remoteMessage .getFrom ());
76
+ Log .d(TAG , " From: " + remoteMessage.from)
88
77
89
78
// Check if message contains a data payload.
90
79
// We will use data messages and hence our messages will be handled here
91
- if (remoteMessage .getData ().size () > 0 ) {
92
- Log .d (TAG , "Message data payload: " + remoteMessage .getData ());
93
-
80
+ if (remoteMessage.data.size > 0 ) {
81
+ Log .d(TAG , " Message data payload: " + remoteMessage.data)
94
82
try {
95
- JSONObject json = new JSONObject (remoteMessage .getData () .toString ());
96
- handleDataMessage (json );
97
- } catch (Exception e ) {
98
- Log .e (TAG , "Exception: " + e .getMessage ());
83
+ val json = JSONObject (remoteMessage.data .toString())
84
+ handleDataMessage(json)
85
+ } catch (e : Exception ) {
86
+ Log .e(TAG , " Exception: " + e.message)
99
87
}
100
88
101
89
// if (/* Check if data needs to be processed by long running job */ true) {
@@ -119,18 +107,16 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
119
107
// Also if you intend on generating your own notifications as a result of a received FCM
120
108
// message, here is where that should be initiated. See sendNotification method below.
121
109
}
122
- // [END receive_message]
123
110
124
-
125
- @ Override
126
- public void onDeletedMessages () {
127
- super .onDeletedMessages ();
111
+ // [END receive_message]
112
+ override fun onDeletedMessages () {
113
+ super .onDeletedMessages()
128
114
}
129
115
130
116
/* *
131
117
* Schedule a job using FirebaseJobDispatcher.
132
118
*/
133
- private void scheduleJob () {
119
+ private fun scheduleJob () {
134
120
// [START dispatch_job]
135
121
// FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
136
122
// Job myJob = dispatcher.newJobBuilder()
@@ -144,84 +130,76 @@ private void scheduleJob() {
144
130
/* *
145
131
* Handle time allotted to BroadcastReceivers.
146
132
*/
147
- private void handleNow () {
148
-
149
- Log .d (TAG , "Short lived task is done." );
133
+ private fun handleNow () {
134
+ Log .d(TAG , " Short lived task is done." )
150
135
}
151
136
152
137
/* *
153
138
* Create and show a simple notification containing the received FCM message.
154
139
*
155
140
* @param messageBody FCM message body received.
156
141
*/
157
- private void sendNotification (String title , String messageBody ) {
158
- Intent intent = new Intent (this , NotificationActivity .class );
159
- intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP );
160
- PendingIntent pendingIntent ;
161
- pendingIntent = PendingIntent .getActivity (this , 0 /* Request code */ ,
162
- intent ,
163
- PendingIntent .FLAG_ONE_SHOT );
164
-
165
- String channelId = getString (R .string .app_name );
166
- Uri defaultSoundUri = RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
167
- NotificationCompat .Builder notificationBuilder =
168
- new NotificationCompat .Builder (this , channelId )
169
- .setSmallIcon (R .drawable .ic_bank )
170
- .setContentTitle (title )
171
- .setContentText (messageBody )
172
- .setAutoCancel (true )
173
- .setSound (defaultSoundUri )
174
- .setContentIntent (pendingIntent );
175
-
176
- NotificationManager notificationManager =
177
- (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
142
+ private fun sendNotification (title : String , messageBody : String ) {
143
+ val intent = Intent (this , NotificationActivity ::class .java)
144
+ intent.addFlags(Intent .FLAG_ACTIVITY_CLEAR_TOP )
145
+ val pendingIntent: PendingIntent
146
+ pendingIntent = PendingIntent .getActivity(
147
+ this , 0 /* Request code */ ,
148
+ intent,
149
+ PendingIntent .FLAG_ONE_SHOT
150
+ )
151
+ val channelId = getString(R .string.app_name)
152
+ val defaultSoundUri = RingtoneManager .getDefaultUri(RingtoneManager .TYPE_NOTIFICATION )
153
+ val notificationBuilder = NotificationCompat .Builder (this , channelId)
154
+ .setSmallIcon(R .drawable.ic_bank)
155
+ .setContentTitle(title)
156
+ .setContentText(messageBody)
157
+ .setAutoCancel(true )
158
+ .setSound(defaultSoundUri)
159
+ .setContentIntent(pendingIntent)
160
+ val notificationManager = getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
178
161
179
162
// Since android Oreo notification channel is needed.
180
163
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
181
- NotificationChannel channel = new NotificationChannel (channelId ,
182
- "Channel human readable title" ,
183
- NotificationManager .IMPORTANCE_DEFAULT );
184
- notificationManager .createNotificationChannel (channel );
164
+ val channel = NotificationChannel (
165
+ channelId,
166
+ " Channel human readable title" ,
167
+ NotificationManager .IMPORTANCE_DEFAULT
168
+ )
169
+ notificationManager.createNotificationChannel(channel)
185
170
}
186
-
187
- notificationManager .notify (0 /* ID of notification */ , notificationBuilder .build ());
171
+ notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build())
188
172
}
189
173
190
174
/* *
191
175
* Handles notification messages.
192
176
*/
193
- private void handleNotification (String title , String message ) {
194
-
195
- }
177
+ private fun handleNotification (title : String , message : String ) {}
196
178
197
179
/* *
198
180
* Handles data messages.
199
181
*/
200
- private void handleDataMessage (JSONObject json ) {
201
- Log .e (TAG , "push json: " + json .toString ());
202
-
182
+ private fun handleDataMessage (json : JSONObject ) {
183
+ Log .e(TAG , " push json: $json " )
203
184
try {
204
- JSONObject data = json .getJSONObject ("data" );
205
-
206
- String title = data .getString ("title" );
207
- String message = data .getString ("message" );
208
- String imageUrl = data .getString ("image" );
209
- String type = data .getString ("type" );
210
- String timestamp = data .getString ("timestamp" );
211
- JSONObject payload = data .getJSONObject ("payload" );
212
-
213
- Log .e (TAG , "title: " + title );
214
- Log .e (TAG , "message: " + message );
215
- Log .e (TAG , "type: " + type );
185
+ val data = json.getJSONObject(" data" )
186
+ val title = data.getString(" title" )
187
+ val message = data.getString(" message" )
188
+ val imageUrl = data.getString(" image" )
189
+ val type = data.getString(" type" )
190
+ val timestamp = data.getString(" timestamp" )
191
+ val payload = data.getJSONObject(" payload" )
192
+ Log .e(TAG , " title: $title " )
193
+ Log .e(TAG , " message: $message " )
194
+ Log .e(TAG , " type: $type " )
216
195
// payload can be used when one needs to show specific notification with some data
217
196
// and process it
218
197
if (payload != null ) {
219
- Log .e (TAG , "payload: " + payload . toString ());
198
+ Log .e(TAG , " payload: $ payload" )
220
199
}
221
- Log .e (TAG , "imageUrl: " + imageUrl );
222
- Log .e (TAG , "timestamp: " + timestamp );
223
-
224
- sendNotification (title , message );
200
+ Log .e(TAG , " imageUrl: $imageUrl " )
201
+ Log .e(TAG , " timestamp: $timestamp " )
202
+ sendNotification(title, message)
225
203
226
204
// Below code can be used when you want to show notification with Image.
227
205
@@ -237,31 +215,38 @@ private void handleDataMessage(JSONObject json) {
237
215
// showNotificationMessageWithBigImage(getApplicationContext(), title, message,
238
216
// timestamp, resultIntent, imageUrl);
239
217
// }
240
-
241
- } catch (JSONException e ) {
242
- Log .e (TAG , "Json Exception: " + e .getMessage ());
243
- } catch (Exception e ) {
244
- Log .e (TAG , "Exception: " + e .getMessage ());
218
+ } catch (e: JSONException ) {
219
+ Log .e(TAG , " Json Exception: " + e.message)
220
+ } catch (e: Exception ) {
221
+ Log .e(TAG , " Exception: " + e.message)
245
222
}
246
223
}
247
224
248
225
/* *
249
226
* Showing notification with text only
250
227
*/
251
- private void showNotificationMessage (Context context , String title , String message ,
252
- String timeStamp , Intent intent ) {
253
- NotificationUtils notificationUtils = new NotificationUtils (context );
254
- intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
255
- notificationUtils .showNotificationMessage (title , message , timeStamp , intent );
228
+ private fun showNotificationMessage (
229
+ context : Context , title : String , message : String ,
230
+ timeStamp : String , intent : Intent
231
+ ) {
232
+ val notificationUtils = NotificationUtils (context)
233
+ intent.flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
234
+ notificationUtils.showNotificationMessage(title, message, timeStamp, intent)
256
235
}
257
236
258
237
/* *
259
238
* Showing notification with text and image
260
239
*/
261
- private void showNotificationMessageWithBigImage (Context context , String title , String message ,
262
- String timeStamp , Intent intent , String imageUrl ) {
263
- NotificationUtils notificationUtils = new NotificationUtils (context );
264
- intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
265
- notificationUtils .showNotificationMessage (title , message , timeStamp , intent , imageUrl );
240
+ private fun showNotificationMessageWithBigImage (
241
+ context : Context , title : String , message : String ,
242
+ timeStamp : String , intent : Intent , imageUrl : String
243
+ ) {
244
+ val notificationUtils = NotificationUtils (context)
245
+ intent.flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
246
+ notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl)
247
+ }
248
+
249
+ companion object {
250
+ private const val TAG = " MifosPayFCM"
266
251
}
267
252
}
0 commit comments