0
votes

I have an android app that sends that is supposed to send a push notification to users. When the notification is received by the user, the user taps on the notification and the app is opened and a url is opened in the android webview.

but my app is not receiving any notification. here is the code

    public class MainActivity extends AppCompatActivity {
         private WebView webView;
         private ProgressDialog dialog;
         private BroadcastReceiver mRegistrationBroadcastReciever;
         private final String CHANNEL_ID="notificcation";        
         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             webView=(WebView)findViewById(R.id.webView);
             webView.getSettings().setJavaScriptEnabled(true);
             webView.setWebChromeClient(new WebChromeClient());
             webView.setWebViewClient(new WebViewClient(){
                  @Override
                  public void onPageFinished(WebView view, String url) {
                      if(dialog.isShowing())
                         dialog.dismiss();
                  }
             });     
             mRegistrationBroadcastReciever=new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context context, Intent intent) {
                      if(intent.getAction().equals(Config.STR_PUSH)){
                          String message=intent.getStringExtra(Config.STR_MESSAGE);
                          showNotification("MSG",message);
                      }    
                  }
             };
             onNewIntent(getIntent());
         }
         @Override
         protected void onNewIntent(Intent intent) {
              dialog=new ProgressDialog(this);
              if(intent.getStringExtra(Config.STR_KEY)!=null){
                  dialog.show();
                  dialog.setMessage("Please Wait");
                  webView.loadUrl(intent.getStringExtra(Config.STR_KEY));    
              }
         }    
         private void showNotification(String title, String message) {
              Intent intent =new Intent(getBaseContext(),MainActivity.class);
              intent.putExtra(Config.STR_KEY,message);
              intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
              PendingIntent contentIntent=PendingIntent.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
              NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID);
              builder.setAutoCancel(true)
              .setWhen(System.currentTimeMillis())
              .setDefaults(Notification.DEFAULT_ALL)
              .setSmallIcon(R.mipmap.ic_launcher_round)
              .setContentTitle(title)
              .setContentText(message)
              .setContentIntent(contentIntent);

              NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
              notificationManager.notify(1,builder.build());
         }
         @Override
         protected void onPause() {
                LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReciever);
              super.onPause();
         }
         @Override
         protected void onResume() {
              super.onResume();
              LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter("registration Complete"));
              LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter(Config.STR_PUSH));
         }
    }

The FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService{
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       handleMessage(remoteMessage.getData().get(Config.STR_KEY));

    }

    private void handleMessage(String message) {
        Intent pushNotification=new Intent(Config.STR_PUSH);
        pushNotification.putExtra(Config.STR_MESSAGE,message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);


    }

}

The Firebase Instance class

public class MyFirebaseIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String token = FirebaseInstanceId.getInstance().getToken();
        sendToServer(token);

    }

    private void sendToServer(String token) {


    }
}
2
Form where you are sending the notification Firebase Console ?Faiz Mir
From the Firebase ConsoleAstute Dlamini

2 Answers

2
votes

Messages sent via the Firebase console are treated as notification message payloads. From your code, you're only handling data message payloads (remoteMessage.getData() which are probably null. You could include a data message payload along with the notification message contents by adding Advanced Option via the Firebase Console.

Also, FirebaseInstanceIdService has been deprecated. Proceed with using onNewToken() in FirebaseMessagingService.

0
votes

First you are using LocalBroadcastManager that's only registered if your MainActivity is currently in the foreground.

Secondly, what kind of message are you sending? They could be data or notification messages. If using data I would get rid of the LocalBroadcastManager and declare the showNotification method in MyFirebaseMessagingService so you can directly show the notification from there.

If you are using notification messages. If your app is in the background the push notification would be handled by the System Tray, and the onMessageReceived would only be called if your App is in the foreground.

Take a look at the docs here: https://firebase.google.com/docs/cloud-messaging/android/receive

The most significant part:

onMessageReceived is provided for most message types, with the following exceptions:

  • Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
  • Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.