当应用程序未运行时,Android通知没有显示其内容

23

这是我的有趣的问题。当应用程序未运行时,来自GCM的Android通知不显示标题和内容(仅显示应用程序名称,并在点击时打开MainActivity)。

但是当应用程序正在运行时,它会成功地显示标题和内容。可能出了什么问题?它以前没有问题,我也没有更改任何内容。

清单:

 <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) {
    }

谢谢。

应用程序运行时的日志记录。

 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}]

应用程序未运行时的日志记录。

(empty, there is no log)

有任何错误日志吗?你能提供你的日志吗? - Android Enthusiast
很遗憾,没有日志。非常有趣。 - Ozgur
在两种情况下,通过以下行返回日志:Log.d("GCMService", data.toString()); - Eric B.
@OzgurGUL 这很奇怪。因为如果 data 为空,那么您的应用程序根本不应该显示任何通知。 - Eric B.
@EricB. 是的,有趣的问题。 - Ozgur
显示剩余2条评论
6个回答

8

找到问题了。我使用的是8.4.0版本(最新)的Play服务。

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

我将版本号降低到8.3.0。它的表现符合预期。


3
这绝对不是一个解决方案。降级版本...我也有类似的问题,我能看到内容但没有标题...请尝试找到一个合适的解决方案,而不是这个权宜之计。谢谢。 - saur
1
我仍在寻找解决方案,但这显然是8.4版本的问题。 - Ozgur

5

取代

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

尝试使用

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

4
简单来说,当在服务器端构建推送时请尝试设置content_available=false。以下是详细解释:
自从Play Services 8.4.0版本以后,文档说明如果你发送了一个同时包含datanotification有效载荷的下行消息,当应用程序在前台或后台时行为会有所不同:
- 在前台,监听器onMessageReceived会被调用,您可以手动处理通知。 - 在后台,系统会自动构建通知,从通知有效载荷中获取titlebody。如果您没有指定它们,则title将填充应用程序名称,而body则为空(这似乎是您的情况)。
在您的情况下,在消息bundle中看到了这种奇怪的东西:notification=Bundle[{e=1}]。我也遇到了同样的问题。此通知有效载荷是自动生成的。我通过在服务器端构建推送时设置content_available=false来成功移除它。这对于使用iOS的用户可能会带来问题,但我没有找到更好的解决方案……试试吧。
这是我引用的Google文档:https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages 希望这能帮到您,再见。

1
我认为你的问题在这一行:
你没有包含这个:
<service
            android:name="com.example.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

1
问题出在GCM 8.4.0版本,即使你的服务器没有发送通知负载,它也会发送一个通知负载。
notification=Bundle[{e=1}

但是,如果您在服务器上添加带有值为 zeroe 字段,则可以使其正常工作。

更多细节请参见我的答案 这里


1
当创建一个通知时,棒棒糖(Lollipop)会在设备窗口顶部创建一个小弹出窗口。使用这种方法,您可以创建一个具有任何自定义布局的新活动,并启动该活动,而不是将通知放在状态栏中。以下是官方文档链接:setFullScreenIntent。请注意,保留HTML标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接