Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. package com.ragshion.ayosekolah.fcm;
  2.  
  3.  
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.media.RingtoneManager;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.support.v4.app.NotificationCompat;
  14. import android.util.Log;
  15.  
  16. import com.google.firebase.messaging.FirebaseMessagingService;
  17. import com.google.firebase.messaging.RemoteMessage;
  18. import com.google.gson.Gson;
  19. import com.google.gson.JsonParser;
  20. import com.google.gson.JsonSyntaxException;
  21. import com.ragshion.ayosekolah.R;
  22. import com.ragshion.ayosekolah.activities.DatazAkunActivity;
  23.  
  24. import java.lang.reflect.Type;
  25. import java.util.List;
  26.  
  27.  
  28. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  29.  
  30. private static final String TAG = "MyFirebaseMsgService";
  31. int value = 0;
  32. private static final String group_notif = "bappeda";
  33.  
  34. String click_action;
  35.  
  36. @Override
  37. public void onMessageReceived(RemoteMessage remoteMessage) {
  38.  
  39. String click_action = remoteMessage.getNotification().getClickAction();
  40. Intent intent = new Intent(click_action);
  41.  
  42. // TODO(developer): Handle FCM messages here.
  43. Log.d(TAG, "From: " + remoteMessage.getFrom());
  44.  
  45. // Check if message contains a data payload.
  46. if (remoteMessage.getData().size() > 0) {
  47. Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  48.  
  49. }
  50.  
  51. // Check if message contains a notification payload.
  52. if (remoteMessage.getNotification() != null) {
  53.  
  54. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  55. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
  56. //click_action = remoteMessage.getNotification().getClickAction();
  57.  
  58. //Intent intent = new Intent(click_action);
  59. //intent.putExtra("tag",remoteMessage.getNotification().getTag());
  60. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
  61. }
  62.  
  63. }
  64.  
  65.  
  66. private void sendNotification(String messageBody, String messageTitle) {
  67. Intent intent = new Intent(this, DatazAkunActivity.class);
  68. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  69. intent.putExtra("pushnotif","yes");
  70. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  71. PendingIntent.FLAG_ONE_SHOT);
  72. String channelId = "RuangSekda";
  73.  
  74.  
  75. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  76.  
  77. Notification newMessageNotification = new NotificationCompat.Builder(this, channelId)
  78. .setSmallIcon(R.drawable.ic_logo)
  79. .setContentTitle(messageTitle)
  80. .setContentText(messageBody)
  81. .setStyle(new NotificationCompat.BigTextStyle()
  82. .bigText(messageBody)
  83. )
  84. .setGroup("RuangSekda")
  85. .setAutoCancel(true)
  86. .setSound(defaultSoundUri)
  87. .setContentIntent(pendingIntent)
  88. .build();
  89.  
  90. NotificationManager notificationManager =
  91. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  92.  
  93. // Since android Oreo notification channel is needed.
  94. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  95. NotificationChannel channel = new NotificationChannel(channelId,
  96. "Channel human readable title",
  97. NotificationManager.IMPORTANCE_DEFAULT);
  98. notificationManager.createNotificationChannel(channel);
  99.  
  100. }
  101.  
  102. notificationManager.notify(0 /* ID of notification */, newMessageNotification);
  103.  
  104.  
  105. }
  106.  
  107. public static <T> List<T> getTeamListFromJson(String jsonString, Type type) {
  108. if (!isValid(jsonString)) {
  109. return null;
  110. }
  111. return new Gson().fromJson(jsonString, type);
  112. }
  113.  
  114. public static boolean isValid(String json) {
  115. try {
  116. new JsonParser().parse(json);
  117. return true;
  118. } catch (JsonSyntaxException jse) {
  119. return false;
  120. }
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement