2014-10-29 3 views
2

Я показываю оповещения с помощью Google Cloud Cloud Messaging в android.it работает на некоторых устройствах и эмуляторах, но не работает на всех устройствах, таких как Samsunng Galaxy Grand и некоторые из моих эмуляторов, он просто отображает значок и название уведомления, пока сообщение пустое.андроид push-уведомление показывает пустое содержимое

mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, MyActivity.class), 0); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this).setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(mes)) 
      .setContentText(mes); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
+0

r u получение любой ошибки – Meenal

+0

Проверьте свой ключ для сообщения на вашей стороне. Это похоже на сервер sidE? – Piyush

+0

нет, я не получаю никаких ошибок. единственная проблема заключается в том, что в некоторых устройствах заголовок уведомления, значок, сообщение отображаются отлично. Но в том же уведомлении будет отображаться только значок, заголовок, но сообщение будет пустым. – Vishal

ответ

0

попробовать, как это может помочь вам

mNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MyActivity.class), 0); 

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(mes)) 
       .setContentText(mes); 

    } else { 
      //code for simple notification 
    } 
+0

Привет, спасибо за ответ, но я замечаю, что если я даю статический текст в ContentText, то он выглядит как должен. Проблема возникает только тогда, когда я использую переменную в то, что она не отображается – Vishal

0

Попробуйте этот код.

private void generateNotification(Context context, String message) { 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, message, when);   
     String title = "New POC Alert";   
     Intent notificationIntent = new Intent(context, null); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 
    } 

// MainFest разрешение

<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="vibrate" /> 
0

Пожалуйста, попробуйте это.

// Put the message into a notification and post it. 
// MainActivity is the activity which you wish to load. Setting setWhen() with 0 will not publish time. 

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.your_app_icon) 
        .setContentTitle(getResources().getText(R.string.your_title)) 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setWhen(0).setPriority(NotificationCompat.PRIORITY_HIGH) 
        .setContentText(msg) 
        .setAutoCancel(true); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 

Надеюсь, у вас есть необходимые разрешения. Код: GCM Reference

+0

Привет, спасибо за ответ, но я замечаю, что если я даю статический текст в ContentText, то он выглядит как должен. Проблема возникает только тогда, когда я использую переменную, тогда она не отображается – Vishal

+0

Вы пытались отлаживать? Надеюсь, вы правильно разобрали его. Я не думаю, что предоставление статических сообщений работает, а передача в качестве параметра - нет. –

Смежные вопросы