Created
August 2, 2020 19:28
-
-
Save ayetolusamuel/74a5f51599606d5c5ab42607ef842986 to your computer and use it in GitHub Desktop.
This file contains 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 lagrangecode.com.alausasabi.webnotifications; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.util.Log; | |
import androidx.annotation.NonNull; | |
import androidx.core.app.NotificationCompat; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
import java.util.Objects; | |
import lagrangecode.com.alausasabi.R; | |
import lagrangecode.com.alausasabi.activity.MainActivity; | |
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
private static final String TAG = "MyFirebaseMessagingServ"; | |
@Override | |
public void onNewToken(@NonNull String s) { | |
super.onNewToken(s); | |
Log.d(TAG, "onNewToken: " + s); | |
} | |
@Override | |
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { | |
super.onMessageReceived(remoteMessage); | |
System.out.println("Remote "); | |
if (remoteMessage != null) { | |
Log.d(TAG, "onMessageReceived: Message successfully"); | |
System.out.println("data received"); | |
sendNotification(remoteMessage); | |
} else { | |
Log.d(TAG, "onMessageReceived: error"); | |
} | |
} | |
private void sendNotification(RemoteMessage remoteMessage) { | |
Intent intent = new Intent(this, SignedInActivity.class); | |
intent.putExtra("webUrl", remoteMessage.getData().get("key")); | |
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.logo) | |
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), | |
R.drawable.gov)) | |
.setContentTitle(remoteMessage.getNotification().getTitle()) | |
.setContentText(remoteMessage.getNotification().getBody()) | |
.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()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The WebView
`package lagrangecode.com.alausasabi.webnotifications;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import lagrangecode.com.alausasabi.R;
public class SignedInActivity extends AppCompatActivity {
}
}`