3

I can't receive my Firebase Notifications when the app is killed or in background.

I signed the APK, and it doesn't work, and when I send my Data notification with JSON the logcat shows me this error:

   broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.example.paolomazza.firebasedemo3 (has extras) }

Given the following Manifest, what could be the cause of this bug? Thanks in Advance

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name="com.example.paolomazza.firebasedemo3.OnBootBroadcastReceiver">
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".FirebaseIDService">
        <intent-filter>
            <action android:name="com.example.paolomazza.firebasedemo3.INSTANCE_ID_EVENT"
                android:stopWithTask="false" />
        </intent-filter>
    </service>

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"
                android:stopWithTask="false" />
        </intent-filter>
    </service>
</application>

And here's the code for showing the notifications

public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();

    //you can get your text message here.
    String text= data.get("my_custom_key");

    System.out.println(text);
    notifies(text);
}

private void notifies(String body){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                    .setSmallIcon(android.R.drawable.ic_menu_help)
                    .setContentTitle("Congratulations!")
                    .setContentText(body);
    Notification mNot= mBuilder.build();
    NotificationManager mNotMan = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    mNotMan.notify(1,mNot);
}
Paolo Mazza
  • 87
  • 11
  • No, when the app is killed in my Android phone it doesn't show the notification. I sent a JSON data notification! – Paolo Mazza Jun 20 '17 at 10:58
  • Can you post your notification generate code where you create you notification in which activity? – Andy Developer Jun 20 '17 at 10:59
  • for sure, I post it in the first message! – Paolo Mazza Jun 20 '17 at 11:02
  • I am not talking about that first message I am talking about the notification generate code when you get the Json payload and the you generate the notification.Did you implement above solution and tried which I was posted. – Andy Developer Jun 20 '17 at 11:05
  • Sorry, I mean I wrote my code in the main message here on this post in stack Overflow! – Paolo Mazza Jun 20 '17 at 11:06
  • Problem is in your notification generate code. You are not using the pending intent in that.Please [see this link](https://developer.android.com/guide/topics/ui/notifiers/notifications.html) – Andy Developer Jun 20 '17 at 11:15
  • You can also refer this stackoverflow [link](https://stackoverflow.com/questions/13902115/how-to-create-a-notification-with-notificationcompat-builder) – Andy Developer Jun 20 '17 at 11:18
  • No, I put the pending intent and when the app is killed it doesn't show the notification (on my adb instead yes). ON my two physical phones I use Marshmallow as OS. The logcat is always the same – Paolo Mazza Jun 20 '17 at 11:35
  • Please check any application in your phone don't restrict the notification this happens in many devices now a days. Also make sure you have to allow notification for the application.For that go to app info inside check the notification section. – Andy Developer Jun 20 '17 at 11:38
  • Already checked, the app is allowed to send notifications – Paolo Mazza Jun 20 '17 at 11:41
  • try https://stackoverflow.com/a/58162451/7579041 this – Amin Pinjari Sep 30 '19 at 06:30

1 Answers1

3

OK, I just solved the problem.

If you're using a Huawei device, or a device that uses a EMOI os, just go to

Advanced settings

than go to

Battery

and

Protected Apps

than select your app, switch it on and you'll be able to receive your notifications in the background!

Paolo Mazza
  • 87
  • 11