gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:41:47 25 4
gpt4 key购买 nike

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

但当应用打开时,它会显示成功标题和内容。可能是什么问题?它运行没有问题,我没有改变任何东西。

list :

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

最佳答案

发现问题。我使用的是 8.4.0 版本(最新)的播放服务。

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

我将版本降低到 8.3.0。它按预期工作。

关于java - 当应用程序未运行时,Android 通知未显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35059614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com