123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package com.example.fcm;
-
- 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.os.Build;
- import android.os.VibrationEffect;
- import android.os.Vibrator;
- import android.util.Log;
-
- import androidx.annotation.RequiresApi;
- import androidx.core.app.NotificationCompat;
- import androidx.work.OneTimeWorkRequest;
- import androidx.work.WorkManager;
-
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
-
- import java.util.Random;
-
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private final String ADMIN_CHANNEL_ID ="admin_channel";
- private static final String TAG = "MyFirebaseMsgService";
- private static final String FIREBASE_URL = "https://fcmfirebaseproject-ececa.firebaseio.com";
-
- /**
- * Called when message is received.
- *
- * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
- */
- // [START receive_message]
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- // [START_EXCLUDE]
- // There are two types of messages data messages and notification messages. Data messages
- // are handled
- // here in onMessageReceived whether the app is in the foreground or background. Data
- // messages are the type
- // traditionally used with GCM. Notification messages are only received here in
- // onMessageReceived when the app
- // is in the foreground. When the app is in the background an automatically generated
- // notification is displayed.
- // When the user taps on the notification they are returned to the app. Messages
- // containing both notification
- // and data payloads are treated as notification messages. The Firebase console always
- // sends notification
- // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
- // [END_EXCLUDE]
-
- // TODO(developer): Handle FCM messages here.
- // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
- final Intent intent = new Intent(this, MainActivity.class);
- NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- int notificationID = new Random().nextInt(3000);
-
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- setupChannels(notificationManager);
- }
-
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this , 0, intent,
- PendingIntent.FLAG_ONE_SHOT);
-
- String title = remoteMessage.getData().get("title");
- String message = remoteMessage.getData().get("message");
- String level = remoteMessage.getData().get("level");
- String delay = remoteMessage.getData().get("delay");
- String duration = remoteMessage.getData().get("duration");
-
- String wholeMessage = message + "," + level + "," + delay + "," + duration;
-
- //MainActivity main = new Vibration();
- assert level != null;
- assert delay != null;
- assert duration != null;
- try {
- triggerVibration(Integer.parseInt(level), Integer.parseInt(delay), Integer.parseInt(duration));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_stat_ic_notification)
- .setContentTitle(title)
- .setContentText(wholeMessage)
- .setAutoCancel(true)
- .setSound(notificationSoundUri)
- .setContentIntent(pendingIntent);
-
- notificationManager.notify(notificationID, notificationBuilder.build());
-
- 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());
-
- if (/* Check if data needs to be processed by long running job */ true) {
- // For long-running tasks (10 seconds or more) use WorkManager.
- scheduleJob();
- } else {
- // Handle message within 10 seconds
- handleNow();
- }
-
- }
-
- // Check if message contains a notification payload.
- if (remoteMessage.getNotification() != null) {
- Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
- }
-
- // Also if you intend on generating your own notifications as a result of a received FCM
- // message, here is where that should be initiated. See sendNotification method below.
- }
- // [END receive_message]
-
-
- // [START on_new_token]
-
- /**
- * Called if InstanceID token is updated. This may occur if the security of
- * the previous token had been compromised. Note that this is called when the InstanceID token
- * is initially generated so this is where you would retrieve the token.
- */
- @Override
- public void onNewToken(String token) {
- Log.d(TAG, "Refreshed token: " + token);
-
- // If you want to send messages to this application instance or
- // manage this apps subscriptions on the server side, send the
- // Instance ID token to your app server.
- sendRegistrationToServer(token);
- }
- // [END on_new_token]
-
- /**
- * Schedule async work using WorkManager.
- */
- private void scheduleJob() {
- // [START dispatch_job]
- OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(MyWorker.class)
- .build();
- WorkManager.getInstance().beginWith(work).enqueue();
- // [END dispatch_job]
- }
-
- /**
- * Handle time allotted to BroadcastReceivers.
- */
- private void handleNow() {
- Log.d(TAG, "Short lived task is done.");
- }
-
- /**
- * Persist token to third-party servers.
- *
- * Modify this method to associate the user's FCM InstanceID token with any server-side account
- * maintained by your application.
- *
- * @param token The new token.
- */
- private void sendRegistrationToServer(String token) {
- // TODO: Implement this method to send token to your app server.
- }
-
- /**
- * Create and show a simple notification containing the received FCM message.
- *
- * @param messageBody FCM message body received.
- */
- private void sendNotification(String messageBody) {
- Intent intent = new Intent(this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
- PendingIntent.FLAG_ONE_SHOT);
-
- String channelId = getString(R.string.default_notification_channel_id);
- Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder =
- new NotificationCompat.Builder(this, channelId)
- .setSmallIcon(R.drawable.ic_stat_ic_notification)
- .setContentTitle(getString(R.string.fcm_message))
- .setContentText(messageBody)
- .setAutoCancel(true)
- .setSound(defaultSoundUri)
- .setContentIntent(pendingIntent);
-
- 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 */, notificationBuilder.build());
- }
-
- @RequiresApi(api = Build.VERSION_CODES.O)
- private void setupChannels(NotificationManager notificationManager){
- CharSequence adminChannelName = "New notification";
- String adminChannelDescription = "Device to devie notification";
-
- NotificationChannel adminChannel;
- adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
- adminChannel.setDescription(adminChannelDescription);
- adminChannel.enableLights(true);
- adminChannel.setLightColor(Color.RED);
- adminChannel.enableVibration(true);
- if (notificationManager != null) {
- notificationManager.createNotificationChannel(adminChannel);
- }
- }
-
- public void triggerVibration(int level, int delay, int duration) throws InterruptedException {
- Log.i("Vibration","triggerVibration");
- Log.i("Vibration","level:" + level);
- Log.i("Vibration","delay:" + delay + "ms");
- Log.i("Vibration","duration:" + duration + "ms");
- Vibrator a = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- try {
- Thread.sleep(delay);
- } catch(InterruptedException ex){
- Thread.currentThread().interrupt();
- }
- a.vibrate(VibrationEffect.createOneShot(duration, level));
- } else {
- //deprecated in API 26
- a.vibrate(2000);
- }
- }
-
- }
|