Reputation: 59
I implemented Push Notification via FCM. When the app gets a new notification from my server, The Notification Panel strip noticed by the icon which I set in NotificationCompat.Builder, But the message not preview as a pop up. I tried to set priority, style, category, but still the notification not shown. When I'm scrolling I can see the notification.
I tried the app on 2 different devices OS (6.0.1 & 5.0.1) Also My backend C# solution - both approches not popup the notification - message and notification
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theApp"
android:versionCode="31"
android:versionName="0.3.4" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.theApp.SplashScreen"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theApp.MainView"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.NoActionBar" >
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
FirebaseMessagingService
package com.theApp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
}
public void showNotification(String message)
{
Intent i =new Intent(this,MainView.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //FLAG_ACTIVITY_CLEAR_TOP
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message)
.setTicker(message)
.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_PROMO);
NotificationManager manager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTI FICATION_SERVICE);
manager.notify(0,builder.build());
}
}
Backend (C#):
// Data message
var contentData = new
{
registration_ids = tokens,
priority = "high",
data = new
{
title = "this is title",
body = "this is body"
}
};
// Notification message
var contentData = new
{
registration_ids = tokens,
priority = "high",
notification = new
{
title = title,
body = message
}
};
Upvotes: 5
Views: 4154
Reputation: 6322
I know this is an older question, but to answer it, you need to change the JSON payload in order for it to be handled differently on the device.
See This Question with regards to why it makes a difference as to the payload being sent.
The short answer is that if you send this as your push notification body to an Android device:
{
"data":{
"title": "hello",
"body": "this is body"
},
"to": "firebase_long_token_goes_here"
}
You will be able to parse it in the onMessageReceived() method and decide whether to or not to show it depending on how you parse the JSON. If you send it the traditional way or the way IOS can parse it:
{
"notification":{
"title": "hello",
"body": "this is body"
},
"to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
}
And the app is in the background, you will have no choice and the system will show it regardless of your desire to hide or not display it.
Upvotes: 0
Reputation: 1166
If you want to read a notification you must call remoteMessage.getNotification()
rather than calling remoteMessage.getData()
.Note that 'notification'
key should be available in your server sent payload in order to parse it as a Notification. getData()
will return a Map only If your server sends a data content. For more information see the FCM guide
Upvotes: 1