0

Updates 1:

Notifications are not received when the app is on the screen, only in the foreground. If the app is running on the screen is happening this:

This is what I've got on the phone screen.

enter image description here

This is the logcat

2019-12-14 17:49:12.959 26000-26083/com.maunexus D/AudioTrack: stop(124): called with 438244 frames delivered
2019-12-14 17:49:18.006 26000-26215/com.maunexus D/FA: Logging event (FE): notification_receive(_nr), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=2966358034792091456, message_device_time(_ndt)=0, _nmc=display, message_name(_nmn)=Yeeey!, message_time(_nmt)=1576338559, message_id(_nmid)=6989073632392201936}]
2019-12-14 17:49:18.191 26000-26215/com.maunexus D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=2966358034792091456, message_device_time(_ndt)=0, _nmc=display, message_name(_nmn)=Yeeey!, message_time(_nmt)=1576338559, message_id(_nmid)=6989073632392201936}]
2019-12-14 17:49:18.239 26000-26215/com.maunexus D/FA: Connected to remote service

I'm trying to implement a push notification service for my app trough the Firebase FCM service, but unfortunately, it doesn't work.

What happens, well nothing in the app, it doesn't crash but also it does not receive any notification I've sent from the Firebase console.

I'm gonna attach all the relevant files from my app.

MainActivity.java

package com.maunexus;

import android.app.Activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;


public class MainActivity extends AppCompatActivity {

    private WebView webView;
    Activity activity;
    private static final String TAG = "MainActivity";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity = this;


        webView = (WebView) findViewById(R.id.webviewid);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setSupportZoom(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://pari365.mg");


        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
//To do//
                            return;
                        }

// Get the Instance ID token//
                        String token = task.getResult().getToken();
                        String msg = getString(R.string.fcm_token, token);
                        Log.d(TAG, msg);

                    }
                });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.super_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_back:
                onBackPressed();
                break;

            case R.id.menu_forward:
                onForwardPressed();
                break;

            case R.id.menu_refresh:
                webView.reload();
                Toast.makeText(this, "Reloading... Please Wait!", Toast.LENGTH_SHORT).show();
                break;


        }
        return super.onOptionsItemSelected(item);
    }


    private void onForwardPressed() {
        if (webView.canGoForward()) {
            webView.goForward();
        } else {
            Toast.makeText(this, "Already there! ;)", Toast.LENGTH_SHORT).show();
        }
    }

    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        }
    }
}

MyFirebaseMessagingService.java.

Two warnings here at .getTitle() and .notify() methods:

Method invocation 'notify'/'getTitle' may produce NullPointerException

   package com.maunexus;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Build;


import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import org.jetbrains.annotations.NotNull;


public class MyFirebaseMessagingService extends FirebaseMessagingService{

    @Override
    public void onMessageReceived(@NotNull RemoteMessage remoteMessage) throws NullPointerException {
        super.onMessageReceived(remoteMessage);
        //Log.i("#DEBUG#", remoteMessage.toString());

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "channel_id",
                    "Channel name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Channel description");

            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }

}

build.Grandle (App)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.maunexus"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-core:17.2.1'
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    implementation 'com.google.firebase:firebase-inappmessaging-display:19.0.2'
    implementation 'com.android.support:multidex:1.0.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
apply plugin: 'com.google.gms.google-services'

The following line was added to strings.xml

 <string name="fcm_token">FCM Token: %s</string>

And finally, few lines I found relevant from the logcat

2019-12-13 23:41:25.894 10009-10009/com.maunexus I/FirebaseInitProvider: FirebaseApp initialization successful

2019-12-13 23:41:29.181 10009-10074/com.maunexus I/FIAM.Headless: Successfully fetched 0 messages from backend
2019-12-13 23:41:31.796 10009-10009/com.maunexus I/chromium: [INFO:CONSOLE(27)] "AltenarSportsbook", source: https://sb1client-static-altenar.biahosted.com/static/skins/pari365.js?v=aca125c500d0eed54e11a4c03bd17a08 (27)
2019-12-13 23:41:37.425 10009-10127/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:41:37.474 10009-10127/com.maunexus I/chatty: uid=10134(com.maunexus) Chrome_InProcGp identical 4 lines
2019-12-13 23:41:37.474 10009-10127/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:41:37.504 10009-10111/com.maunexus I/libOpenSLES: Emulating old channel mask behavior (ignoring positional mask 0x3, using default mask 0x3 based on channel count of 2)
2019-12-13 23:41:47.509 10009-10111/com.maunexus D/AudioTrack: stop(53): called with 440356 frames delivered

2019-12-13 23:59:29.115 10438-10438/? E/com.maunexus: Unknown bits set in runtime_flags: 0x8000
2019-12-13 23:59:34.099 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:34.100 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:34.335 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:34.335 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:34.552 10438-10498/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:34.553 10438-10498/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:35.345 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:35.349 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:44.720 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:44.763 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:45.097 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef
2019-12-13 23:59:45.098 10438-10553/com.maunexus E/eglCodecCommon: glUtilsParamSize: unknow param 0x000088ef

Thank You very much for your time to check this question. Please help me to fix the issue(s) here.

  • 1
    In `MyFirebaseMessagingService` you are passing `message` but all access is for `remoteMessage`. – Tigger Dec 13 '19 at 22:10
  • try to send the message to a topic, check how you can subscribe your device to a topic then send a notification to that topic – MoTahir Dec 13 '19 at 22:30
  • @Tigger fixed, thanks, but it wasn't the problem. –  Dec 13 '19 at 23:01
  • In `onMessageReceived`, comment out `super.onMessageReceived(remoteMessage)` and add `Log.i("#DEBUG#", message.toString());`. If you changed `message` to `remoteMessage`, use that instead. If you do not see this in your Logcat, then the message is not received at all. – Tigger Dec 13 '19 at 23:56
  • @Tigger Please check updates above in the question body. –  Dec 14 '19 at 13:43
  • I think something is wrong with the channel id, which I'm not gonna use it... –  Dec 14 '19 at 13:52
  • have you created the channel before sending the notification? – Luciano Ferruzzi Dec 14 '19 at 15:58
  • No, it isnt optional? –  Dec 14 '19 at 15:58
  • @adrian you need to create a channel for API 26 or Higher see this https://stackoverflow.com/questions/45711925/failed-to-post-notification-on-channel-null-target-api-is-26 – Deepak Kumar Dec 14 '19 at 16:06
  • @deepakkumar the app is now crash when it is trying to receive a notification. I add that block in the onMessageReceived method, I've created 3 variables String title and message and a Context ctx. It is right? –  Dec 14 '19 at 16:51
  • @adrian in which line it's crashing. Also see this https://stackoverflow.com/questions/45711925/failed-to-post-notification-on-channel-null-target-api-is-26 – Deepak Kumar Dec 14 '19 at 18:53
  • Does this answer your question? [Failed to post notification on channel "null" Target Api is 26](https://stackoverflow.com/questions/45711925/failed-to-post-notification-on-channel-null-target-api-is-26) – Tigger Dec 14 '19 at 20:49
  • @Tigger no, it doesn't. The app is crashes using that method. Or I do not implement it correctly. –  Dec 14 '19 at 21:01

1 Answers1

0

Try creating the notification channel before posting the notification:

This is just a sample, please, create constants for the channel ID and resources for the name and description.

MyFirebaseMessagingService.java

package com.maunexus;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import android.os.Build;

//import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    RemoteMessage remoteMessage;

    @Override
    public void onMessageReceived(@Nullable RemoteMessage message) {
        super.onMessageReceived(remoteMessage);

        createNotificationChannel();
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }


    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "channel_id",
                    "Channel name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Channel description");

            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
}
Luciano Ferruzzi
  • 1,042
  • 9
  • 20
  • I forgot a ";" and put a wrong condition for the creation of the channel, please, try it now. – Luciano Ferruzzi Dec 14 '19 at 17:31
  • No :( The app is compiled with no error. but I get the same message on the screen and the notification is not received. –  Dec 14 '19 at 18:07
  • Also, please check again MyFibaseMessagingService.java update on the question above. I remove the line where remoteMessage is created and replace "message" from method condition with remoteMessage. –  Dec 14 '19 at 18:11