Android 当应用不存在时,通知不显示其内容 运行
Android notification is not showing it's content when app is not running
这是我感兴趣的问题。 Android 来自 GCM 的通知不显示标题和内容( 仅 显示应用名称,点击时打开 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)
而不是
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
尝试使用
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mBuilder.build());
找到问题了。我使用的是 8.4.0 版本(最新)的播放服务。
compile 'com.google.android.gms:play-services-gcm:8.4.0' //GCM
我把版本降到了8.3.0
。它按预期工作。
我认为您的问题出在这一行:
你没有包括这个:
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
Lollipop 通过在创建 Notification 时在设备 window 顶部创建一个小弹出窗口来稍微改变这一点。
这是官方文档:setFullScreenIntent
使用此方法,您可以创建一个新的 Activity 并启动它,而不是放置 Notification 在状态栏中。
简而言之:在服务器端构建推送时尝试设置 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
希望对你有帮助,再见
问题出在 GCM 8.4.0 版本上,它会发送一个通知负载,即使您没有在您的服务器中发送它也是如此。
notification=Bundle[{e=1}
但是,如果您在服务器中添加此 e
字段且值为 zero
,它将起作用。
有关详细信息,请参阅我的回答 here。
这是我感兴趣的问题。 Android 来自 GCM 的通知不显示标题和内容( 仅 显示应用名称,点击时打开 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)
而不是
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
尝试使用
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mBuilder.build());
找到问题了。我使用的是 8.4.0 版本(最新)的播放服务。
compile 'com.google.android.gms:play-services-gcm:8.4.0' //GCM
我把版本降到了8.3.0
。它按预期工作。
我认为您的问题出在这一行:
你没有包括这个:
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
Lollipop 通过在创建 Notification 时在设备 window 顶部创建一个小弹出窗口来稍微改变这一点。
这是官方文档:setFullScreenIntent
使用此方法,您可以创建一个新的 Activity 并启动它,而不是放置 Notification 在状态栏中。
简而言之:在服务器端构建推送时尝试设置 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
希望对你有帮助,再见
问题出在 GCM 8.4.0 版本上,它会发送一个通知负载,即使您没有在您的服务器中发送它也是如此。
notification=Bundle[{e=1}
但是,如果您在服务器中添加此 e
字段且值为 zero
,它将起作用。
有关详细信息,请参阅我的回答 here。