Quick question:
Am I able to receive notification to default android's Notifications
menu (that appears when you swipe status bar down) when my app is not running at all (but installed). If yes - what do I do?
Detailed description:
I have implemented Firebase notification into my android app. Here's how:
Manifest.xml:
<service
android:name=".utils.FireBase">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".utils.FireBaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
FireBaseService.java:
public class FireBaseService extends FirebaseMessagingService {
private static final String TAG = FireBaseService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("message"));
}
}
FireBase.java
public class FireBase extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// I used this token in rest.
Log.e("FirebaseToken", FirebaseInstanceId.getInstance().getToken());
}
}
Fine, now I want to send notification:
curl -X POST --data "$DATA" https://fcm.googleapis.com/fcm/send --header "Content-Type:application/json" --header "Authorization:key=$KEY"
$DATA:
{
"notification":{
"body": "My notification!"
},
"to": "KEY_FROM:FirebaseInstanceId.getInstance().getToken()"
}
Response:
{
"multicast_id": 7830342176959972827,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1485706467606270%8a47512d8a475125"
}
]
}
Now, when my app is Running I'm able to receive notifications. When my app goes background - notifications goes to Android notifications
. Now I close my application (it's neither in foreground nor in background) (By closing I mean - press SQUARE
hardware button -> swipe app left, so my app not in task manager anymore). And I want to receive a notification from firebase into Android notifications
menu. But nothing happens when I call rest service. I don't care about layout or so I just want the user to be notified.
I tried notifications with different devices:
- Real android device: Xiaomi mi4c based on android 5.1.
- Genymotion emulator based on android 7.0 + playmarket installed.
- Genymotion emulator based on andoriod 4.4 + playmarket installed.
Notifications just don't show at all. I observed console, I didn't even find logs about them. I read a lot of stackoverflow posts and as I understand right I should be able to do so.
How do I archive this goal? If I'm not able to receive notification with firebase - maybe I should use GCM instead?
Best regards,