Created
December 20, 2018 11:41
-
-
Save arthtilva/23837e7e457421665f214711388da88b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blinddate.fcm; | |
/** | |
* Created by arthtilva on 15-08-2016. | |
*/ | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.Color; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.util.Log; | |
import com.blinddate.R; | |
import com.blinddate.helper.TableHelper; | |
import com.blinddate.main.DashboardActivity; | |
import com.blinddate.utils.Constants; | |
import com.blinddate.utils.StoreUserData; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
static int notificationId = 0; | |
private static final String TAG = "MyFirebaseMsgService"; | |
// NotificationCompat.InboxStyle inboxStyle; | |
private StoreUserData storeUserData; | |
public MyFirebaseMessagingService() { | |
// inboxStyle = new NotificationCompat.InboxStyle(); | |
} | |
@Override | |
public void onNewToken(String s) { | |
super.onNewToken(s); | |
new StoreUserData(this).setString(Constants.USER_FCM, s); | |
} | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
// TODO(developer): Handle FCM messages here. | |
Log.d(TAG, "From: " + remoteMessage.getFrom()); | |
//messageArray | |
storeUserData = new StoreUserData(this); | |
if (remoteMessage.getData().size() > 0) { | |
Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message")); | |
try { | |
String fromUserId = new JSONObject(remoteMessage.getData().get("messageArray")).getString("fromUserId"); | |
if (!new TableHelper().isBlocked(this, fromUserId) | |
&& !storeUserData.getString(Constants.USER_NAME).toLowerCase().equals(fromUserId.toLowerCase())) { | |
sendNotification(remoteMessage.getData().get("message"), new JSONObject(remoteMessage.getData().get("messageArray")).getString("fromUserId")); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (remoteMessage.getNotification() != null) { | |
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); | |
try { | |
// String fromUserId = new JSONObject(remoteMessage.getData().get("messageArray")).getString("fromUserId"); | |
// if (!new TableHelper().isBlocked(this, fromUserId)) { | |
// sendNotification(remoteMessage.getNotification().getBody(), new JSONObject(remoteMessage.getNotification().get("messageArray")).getString("fromUserId")); | |
// } | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void sendNotification(String messageBody, String groupKey) { | |
Intent intent = new Intent(this, DashboardActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, | |
PendingIntent.FLAG_ONE_SHOT); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
String channelId = "Chat"; | |
int importance = NotificationManager.IMPORTANCE_HIGH; | |
CharSequence channelName = "Blind Date"; | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance); | |
notificationChannel.enableLights(true); | |
notificationChannel.setLightColor(Color.RED); | |
notificationChannel.enableVibration(true); | |
notificationChannel.setVibrationPattern(new long[]{100, 200}); | |
notificationManager.createNotificationChannel(notificationChannel); | |
} | |
// inboxStyle.setBigContentTitle(getResources().getString(R.string.app_name)); | |
// inboxStyle.addLine(messageBody); | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) | |
.setSmallIcon(getNotificationIcon()) | |
.setContentTitle(getResources().getString(R.string.app_name)) | |
.setContentText(messageBody) | |
.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)) | |
// .setGroup(groupKey) | |
.setAutoCancel(true) | |
.setVibrate(new long[]{100, 200}) | |
.setSound(defaultSoundUri) | |
// .setNumber(++notificationId) | |
// .setStyle(inboxStyle) | |
.setContentIntent(pendingIntent); | |
notificationId++; | |
notificationManager.notify(notificationId, notificationBuilder.build()); | |
} | |
private int getNotificationIcon() { | |
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); | |
return useWhiteIcon ? R.drawable.ic_notification : R.mipmap.ic_launcher; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment