Browse Source

FCM Service

pull/1/head
isra el 4 years ago
parent
commit
59caf4f0a5
  1. 2
      app/build.gradle
  2. 7
      app/src/main/AndroidManifest.xml
  3. 3
      app/src/main/java/com/vernu/sms/activities/MainActivity.java
  4. 80
      app/src/main/java/com/vernu/sms/services/FCMService.java
  5. 2
      app/src/main/res/layout/activity_main.xml

2
app/build.gradle

@ -38,4 +38,6 @@ dependencies {
implementation platform('com.google.firebase:firebase-bom:29.2.1') implementation platform('com.google.firebase:firebase-bom:29.2.1')
implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-messaging-directboot'
} }

7
app/src/main/AndroidManifest.xml

@ -9,8 +9,13 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.SMSGateway"> android:theme="@style/Theme.SMSGateway">
<service
android:name=".services.FCMService"
android:exported="false"
android:directBootAware="true">
</service>
<activity <activity
android:name="com.vernu.sms.MainActivity"
android:name="com.vernu.sms.activities.MainActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

3
app/src/main/java/com/vernu/sms/MainActivity.java → app/src/main/java/com/vernu/sms/activities/MainActivity.java

@ -1,4 +1,4 @@
package com.vernu.sms;
package com.vernu.sms.activities;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
@ -15,6 +15,7 @@ import android.widget.Switch;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.vernu.sms.R;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {

80
app/src/main/java/com/vernu/sms/services/FCMService.java

@ -0,0 +1,80 @@
package com.vernu.sms.services;
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 androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.vernu.sms.R;
import com.vernu.sms.activities.MainActivity;
public class FCMService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "N1";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
sendNotification("data msg", "msg boody");
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sendNotification("notif msg", "msg body");
}
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
}
/* build and show notification */
private void sendNotification(String title, 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 = DEFAULT_NOTIFICATION_CHANNEL_ID;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, DEFAULT_NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.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());
}
}

2
app/src/main/res/layout/activity_main.xml

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".activities.MainActivity">
<Switch <Switch
android:id="@+id/gatewaySwitch" android:id="@+id/gatewaySwitch"

Loading…
Cancel
Save