firebase cloud messaging Android push notification not working

firebase cloud messaging Android push notification not working



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)









Form where you are sending the notification Firebase Console ?

– Faiz Mir
Sep 7 '18 at 4:27






From the Firebase Console

– Astute Dlamini
Sep 7 '18 at 4:31




2 Answers
2



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.


notification


data


remoteMessage.getData()


data


notification



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


FirebaseInstanceIdService


onNewToken()


FirebaseMessagingService



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


LocalBroadcastManager



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:


onMessageReceived






in the console, i send also the custom data, which includes the url the app will open after clicking on the notification

– Astute Dlamini
Sep 7 '18 at 4:35






If you send both notification and data, then onMessageReceived will only be called if your app is on the foreground (current active app) else the system tray will handle it and display the notification according to what you set in the console.

– sebasira
Sep 7 '18 at 4:37






even when the app is in foreground notification is not recieved by the emulator. genymotion

– Astute Dlamini
Sep 7 '18 at 4:41






Have you connect your Android App to a FirebaseApp? Do you have the correct .json file? Try as I say removing the LocalBroadcastManager. Also add some logging to the onMessageReceived to check if it really arrives there

– sebasira
Sep 7 '18 at 4:43


.json



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)