Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app getting Push Notification via FCM but not display the message

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
                }
            };
like image 567
koko Avatar asked Nov 08 '22 11:11

koko


1 Answers

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

like image 173
TMtech Avatar answered Nov 14 '22 22:11

TMtech