这是我有趣的问题。当应用未运行时,来自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)
答案 0 :(得分:7)
发现问题。我正在使用8.4.0版本(最新)的播放服务。
compile 'com.google.android.gms:play-services-gcm:8.4.0' //GCM
我将版本缩减为8.3.0
。它按预期工作。
答案 1 :(得分:3)
而不是
// ~ 2 : 3
// ~ 1 : 1
// ~ 4 : 3
// ~ 16 : 9
// ~ 8 : 3
// ~ 87 : 28
//@property imageShape
//@abstract A TVContentItemImageShape value describing the intended aspect ratio or shape of the image.
//@discussion For Top Shelf purposes: the subset of values which are //valid in this property, for TVContentItems in the topShelfItems property //of the TVTopShelfProvider, depends on the value of the topShelfStyle //property of the TVTopShelfProvider:
TVTopShelfContentStyleInset:
valid: TVContentItemImageShapeExtraWide
TVTopShelfContentStyleSectioned:
valid: TVContentItemImageShapePoster
valid: TVContentItemImageShapeSquare
valid: TVContentItemImageShapeHDTV
尝试使用
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
答案 2 :(得分:3)
简而言之:在构建服务器端推送时尝试设置content_available=false
。解释如下。
这发生在版本8.4.0的播放服务中。
文档说如果您发送带有data
和notification
有效负载的下游消息,则如果应用程序位于前台或后台,行为会发生变化:
onMessageReceived
,您可以手动处理通知title
和body
自动构建通知。如果您没有指定它们,则title
会填充应用程序名称,而body
会留空(这似乎就是您的情况)。在你的情况下,我在消息包中看到了这个奇怪的事情notification=Bundle[{e=1}]
我遇到了同样的问题。此通知有效负载是自生成的。我设法在服务器端构建推送时通过设置content_available=false
来设法删除它。如果您还在使用iOS,这是一个问题,但我没有找到更好的解决方案......试试看。
这里是我引用的Google文档:https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages
希望它有所帮助,再见
答案 3 :(得分:1)
我认为你的问题就在这一行:
你没有包括这个:
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
答案 4 :(得分:1)
当创建通知时,Lollipop会在设备窗口顶部创建一个小弹出框,从而稍微改变一下。
以下是官方文档:setFullScreenIntent
使用此方法,您可以使用所需的任何自定义布局创建新的活动并启动该方法,而不是将通知放在状态栏中。
答案 5 :(得分:1)
问题出在GCM 8.4.0版本上,即使您没有在服务器中发送,它也会发送通知有效负载。
notification=Bundle[{e=1}
但是,如果您在服务器中添加值为e
的{{1}}字段,则该字段将有效。
有关详细信息,请回答here。