This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
GCMListenerService.java
88 lines (70 loc) · 3.18 KB
/
GCMListenerService.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
package com.twilio.chat.demo;
import java.util.HashMap;
import com.google.android.gms.gcm.GcmListenerService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.twilio.chat.ChatClient;
import com.twilio.chat.NotificationPayload;
public class GCMListenerService extends GcmListenerService
{
private static final Logger logger = Logger.getLogger(GCMListenerService.class);
@Override
public void onMessageReceived(String from, Bundle data)
{
logger.d("onMessageReceived for GCM");
NotificationPayload payload = new NotificationPayload(data);
ChatClient client = TwilioApplication.get().getBasicClient().getChatClient();
if (client != null) {
client.handleNotification(payload);
}
NotificationPayload.Type type = payload.getType();
if (type == NotificationPayload.Type.UNKNOWN) return; // Ignore everything we don't support
String title = "Twilio Notification";
if (type == NotificationPayload.Type.NEW_MESSAGE)
title = "Twilio: New Message";
if (type == NotificationPayload.Type.ADDED_TO_CHANNEL)
title = "Twilio: Added to Channel";
if (type == NotificationPayload.Type.INVITED_TO_CHANNEL)
title = "Twilio: Invited to Channel";
if (type == NotificationPayload.Type.REMOVED_FROM_CHANNEL)
title = "Twilio: Removed from Channel";
// Set up action Intent
Intent intent = new Intent(this, MessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String cSid = payload.getChannelSid();
if (!"".contentEquals(cSid)) {
intent.putExtra(Constants.EXTRA_CHANNEL_SID, cSid);
}
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(payload.getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setColor(Color.rgb(214, 10, 37))
.build();
String soundFileName = payload.getSound();
if (getResources().getIdentifier(soundFileName, "raw", getPackageName()) != 0) {
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/raw/" + soundFileName);
notification.defaults &= ~Notification.DEFAULT_SOUND;
notification.sound = sound;
logger.d("Playing specified sound "+soundFileName);
} else {
notification.defaults |= Notification.DEFAULT_SOUND;
logger.d("Playing default sound");
}
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}