Skip to content

Instantly share code, notes, and snippets.

@ayetolusamuel
Created August 2, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayetolusamuel/74a5f51599606d5c5ab42607ef842986 to your computer and use it in GitHub Desktop.
Save ayetolusamuel/74a5f51599606d5c5ab42607ef842986 to your computer and use it in GitHub Desktop.
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());
}
}
@ayetolusamuel
Copy link
Author

ayetolusamuel commented Aug 2, 2020

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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signed_in);



    Intent intent = getIntent();
   if(intent != null){

    String result = intent.getStringExtra("webUrl");
    System.out.println("Result "+result);

}

}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment