Android notification is not showing it's conte

2019-02-02 10:45发布

问题:

Here is my interesting problem. Android notification that comes from GCM is not showing title and content (just shows App Name, and when click, open the MainActivity) when app is not running.

But when the app is open, it's showing successfully title and content. What can be the problem? It was running without problem and I didn't change anything.

Manifest:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.package.xxx.permission.C2D_MESSAGE" />
    <permission android:name="com.package.xxx.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.package.xxx" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Service.GcmService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

GcmService.java:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.google.android.gms.gcm.GcmListenerService;
import com.package.xxx.Activity.ReadNormal;
import com.package.xxx.R;


public class GcmService extends GcmListenerService {

    public GcmService() {

    }

    @Override
    public void onMessageReceived(String from, Bundle data) {

        Log.d("GCMService", data.toString());

        String type = data.getString("type", "");

        if(type.equals("news")) {
           showNewsNotification(data);
        }

    }

    private void showNewsNotification(Bundle data) {

        String neId = data.getString("neId");

        if(TextUtils.isEmpty(neId)) {
            return;
        }

        int id = Integer.valueOf(neId);

        NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setContentTitle(data.getString("neTi"))
                .setContentText("Click to read more.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true);

        Intent i = new Intent();
        i.putExtra("neSi", data.getString("neSi"));
        i.putExtra("neUr", data.getString("neUr"));
        i.putExtra("neTi", data.getString("neTi"));
        i.putExtra("neIm", data.getString("neIm"));
        i.putExtra("neId", id);
        i.setClass(this, ReadNormal.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        /***/
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pi);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());

    }

    @Override
    public void onDeletedMessages() {

    }

    @Override
    public void onMessageSent(String msgId) {
    }

    @Override
    public void onSendError(String msgId, String error) {
    }

Thank you.

Logs when app is running.

 D/GCMService: Bundle[{neId=4663755, neIm=http://icdn.posta.com.tr/editor/HD/30/1/2016/fft2mm7549077.jpg, neSi=Posta, neTi=Erdoğan: Rusya sonucuna katlanır, neUr=http://www.posta.com.tr/turkiye/HaberDetay/Erdogan--Rusya-sonucuna-katlanir.htm?ArticleID=324647, type=news, notification=Bundle[{e=1}], collapse_key=com.tekmobil.guncelhaber}]

Logs when app is NOT running.

(empty, there is no log)

回答1:

Found the problem. I was using 8.4.0 version (up-to-date) of play services.

compile 'com.google.android.gms:play-services-gcm:8.4.0' //GCM

I reduced the version to 8.3.0. It works as expected.



回答2:

Instead of

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());

try using

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mBuilder.build());


回答3:

In brief: try setting content_available=false when building the push on server side. The explanation follows.

This happens from version 8.4.0 of play services. The documentation says that if you send a downstream message with both data and notification payload, the behavior changes if the App is in foreground or in background:

  • in foreground the listener onMessageReceived is called and you can manually handle your notification
  • in background the notification is automatically built by the system by taking title and body from the notification payload. If you don't specify them, the title is filled with application name and the body is left empty (and that's seems your case).

In your case I saw, in the message bundle, this strange thing notification=Bundle[{e=1}] I encountered the same problem. This notification payload is self generated. I managed to remove it by setting content_available=false when building the push on server side. This is a problem if you are also working with iOS, but I didn't find any better solution...try it out.

Here the google doc I cited: https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

Hope it helps, bye



回答4:

I think your issue is in this line:

you have not included this:

<service
            android:name="com.example.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>


回答5:

Lollipop changes this slightly by creating a small pop up at the top of the device window when a Notification is created.

Here's a Official documentation: setFullScreenIntent

Using this method, you can create a new Activity with any custom layout you want and launch that instead of placing the Notification in the status bar.



回答6:

The problem is with the GCM 8.4.0 version, it is sending a notification payload even if you don't send it in your server.

notification=Bundle[{e=1}

But if you add this e field with value zero in your server it will work.

For more details se my answer here.