推送通知仅显示没有任何文字的应用徽标。只有现有通知时才会显示这些字词。仅在Samsung 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);
答案 0 :(得分:0)
我想说使代码更加模块化和有条理。通过查看代码很难发现问题。看起来你已经添加但它们的顺序不正确(也许)。
sendNotification
内的onHandleIntent
等方法会在收到通知后立即触发。以下是一个示例:
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());
}
希望这有助于!!