推送通知仅显示应用程序徽标而没有任何文本
Posted
技术标签:
【中文标题】推送通知仅显示应用程序徽标而没有任何文本【英文标题】:Push notification showing only app logo without any text 【发布时间】:2015-08-12 06:16:35 【问题描述】:推送通知仅显示应用程序徽标而没有任何文字。仅当存在现有通知时才会显示这些文字。此问题仅在三星 Galaxy Note 3 (Kitkat 4.4.2) 设备中出现。请帮忙
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
NotificationCompat.BigTextStyle notiStyle1 = null;
NotificationCompat.BigPictureStyle notiStyle2 = null;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.custom_notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notiStyle1 = new
NotificationCompat.BigTextStyle();
mBuilder.setSmallIcon(R.drawable.ic_launcher_small);
else
notiStyle2 = new
NotificationCompat.BigPictureStyle();
mBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationIndex = EntraderApplication.getApplication()
.getNOTIFICATION_ID();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
System.out.println("Status : " + status + ", message : " + message
+ ", content : " + content);
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (status.equalsIgnoreCase("1") || status.equalsIgnoreCase("2")
|| status.equalsIgnoreCase("3"))
try
Log.e("DONT MOVE------>", "in else part");
TradeEntry entry = new ObjectMapper().readValue(content,
TradeEntry.class);
entry.toString();
bundle.putSerializable(
CustomerHomeFragment.CustomerHomeFragment_data, entry);
bundle.putString(
CustomerHomeFragment.CustomerHomeFragment_data_type,
status);
bundle.putInt(Pref_Noti_Clicked, msg_counter);
//bundle.put
// notificationIntent.putExtra(Pref_Noti_Clicked, msg_counter);
System.out.println("Added type : "+ status);
notificationIntent.putExtras(bundle);
catch (Exception e)
e.printStackTrace();
PendingIntent intent = PendingIntent.getActivity(context, notificationIndex,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setTicker(getString(R.string.app_name));
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle(getString(R.string.app_name));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mBuilder.setStyle(notiStyle1);
else
mBuilder.setStyle(notiStyle2);
mBuilder.setContentIntent(intent);
if (EntraderApplication.getApplication().getSharePreference()
.getBoolean(context.getString(R.string.pref_vibration), true))
mBuilder.setVibrate(new long[] 1000, 1000 );
if (EntraderApplication
.getApplication()
.getSharePreference()
.getBoolean(context.getString(R.string.pref_notification), true))
mBuilder.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
remoteViews.setTextViewText(R.id.text, message);
mBuilder.setContent(remoteViews);
Notification notification = mBuilder.build();
mNotificationManager.notify(notificationIndex, notification);
【问题讨论】:
api.asm.skype.com/s/i?0-weu-d4-5cc78c58b17a3456e60a1645292d03cb - 以上问题的截图 您应该提供一个代码 sn-p。而且我无法查看此链接,因为我的公司不允许这样做。 请复制创建要显示的通知的代码。我想您可能正在尝试使用已弃用的代码。 【参考方案1】:我想说让代码更加模块化和有条理。仅通过查看代码很难发现问题。似乎您已添加,但它们的顺序不正确(也许)。
在onHandleIntent
中有诸如sendNotification
之类的方法,一旦收到通知就会被触发。下面是一个示例:
protected void onHandleIntent(Intent intent)
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty())
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType))
sendNotification("Send error: " + extras.toString());
else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType))
sendNotification("Deleted messages on server: " +
extras.toString());
else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType))
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification(extras.toString());
Log.i(TAG, "Received: " + extras.toString());
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
private void sendNotification(String msg)
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.checkmark)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
希望这有帮助!
【讨论】:
以上是关于推送通知仅显示应用程序徽标而没有任何文本的主要内容,如果未能解决你的问题,请参考以下文章
仅使用 phonegap 推送通知而不使用任何 .java 文件