Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ragshion.ayosekolah.fcm;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Build;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import com.google.gson.Gson;
- import com.google.gson.JsonParser;
- import com.google.gson.JsonSyntaxException;
- import com.ragshion.ayosekolah.R;
- import com.ragshion.ayosekolah.activities.DatazAkunActivity;
- import java.lang.reflect.Type;
- import java.util.List;
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private static final String TAG = "MyFirebaseMsgService";
- int value = 0;
- private static final String group_notif = "bappeda";
- String click_action;
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- String click_action = remoteMessage.getNotification().getClickAction();
- Intent intent = new Intent(click_action);
- // TODO(developer): Handle FCM messages here.
- Log.d(TAG, "From: " + remoteMessage.getFrom());
- // Check if message contains a data payload.
- if (remoteMessage.getData().size() > 0) {
- Log.d(TAG, "Message data payload: " + remoteMessage.getData());
- }
- // Check if message contains a notification payload.
- if (remoteMessage.getNotification() != null) {
- Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
- Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
- //click_action = remoteMessage.getNotification().getClickAction();
- //Intent intent = new Intent(click_action);
- //intent.putExtra("tag",remoteMessage.getNotification().getTag());
- sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
- }
- }
- private void sendNotification(String messageBody, String messageTitle) {
- Intent intent = new Intent(this, DatazAkunActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("pushnotif","yes");
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
- PendingIntent.FLAG_ONE_SHOT);
- String channelId = "RuangSekda";
- Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- Notification newMessageNotification = new NotificationCompat.Builder(this, channelId)
- .setSmallIcon(R.drawable.ic_logo)
- .setContentTitle(messageTitle)
- .setContentText(messageBody)
- .setStyle(new NotificationCompat.BigTextStyle()
- .bigText(messageBody)
- )
- .setGroup("RuangSekda")
- .setAutoCancel(true)
- .setSound(defaultSoundUri)
- .setContentIntent(pendingIntent)
- .build();
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- // Since android Oreo notification channel is needed.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel channel = new NotificationChannel(channelId,
- "Channel human readable title",
- NotificationManager.IMPORTANCE_DEFAULT);
- notificationManager.createNotificationChannel(channel);
- }
- notificationManager.notify(0 /* ID of notification */, newMessageNotification);
- }
- public static <T> List<T> getTeamListFromJson(String jsonString, Type type) {
- if (!isValid(jsonString)) {
- return null;
- }
- return new Gson().fromJson(jsonString, type);
- }
- public static boolean isValid(String json) {
- try {
- new JsonParser().parse(json);
- return true;
- } catch (JsonSyntaxException jse) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement