2016-07-26 80 views
0

我在我的應用程序中使用GCM推送通知。但我沒有收到短信和圖標。它只顯示白色背景。通知文本在Android的通知欄中不可見?

這是我們在我的應用程序

public void onMessageReceived(String from, Bundle data) { 

     //Getting the message from the bundle 
     String message = data.getString("message"); 
     //Displaying a notiffication with the message 
     String body = null; 
     String title = null; 
     try{ 
      String notificationJSONString = data.getString("notification"); 
      //then you can parse the notificationJSONString into a JSON object 
      JSONObject notificationJSON = new JSONObject(notificationJSONString); 
      body = notificationJSON.getString("body"); 
      title = notificationJSON.getString("title"); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     // sendNotification(message); 
     sendNotification(body,title); 
    } 

    //This method is generating a notification and displaying the notification 
    private void sendNotification(String message,String title) { 
     Intent intent = new Intent(this, NavigationDrawerActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra("firsttab","notify"); 
     int requestCode = 0; 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 
     NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) 
       // .setSmallIcon(R.mipmap.philips_launcher) 
       .setSmallIcon(getNotificationIcon()) 
       .setContentTitle(title) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(sound) 
       .setColor(Color.parseColor("#0089C4")) 
       .setStyle(inboxStyle) 
       // .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
       .setContentIntent(pendingIntent); 
     /* if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      builder.setSmallIcon(R.drawable.icon_transperent); 
     } else { 
      builder.setSmallIcon(R.drawable.icon); 
     }*/ 

     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, noBuilder.build()); //0 = ID of notification 
    } 

    private int getNotificationIcon() { 
     boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); 
     return useWhiteIcon ? R.drawable.not_icon : R.mipmap.philips_launcher; 
    } 

請幫我解決這個issue.any幫助接收通知代碼將在先進的理解。 我應用了很多在stackoverflow中給出的代碼,但仍面臨同樣的問題。

+0

什麼是您的GCM發佈請求? –

+0

我們在messagereceive函數中獲得瞭如下數據 – Anand

+0

Bundle [{notification = Bundle [{sound2 = 1,e = 1,body = heyyy,icon =,badge = 1, sound = default,title = Philips CORTA-NEW Notification ,vibrate = 1}],collapse_key = com.philips.ordertracker}] – Anand

回答

0

在消息字符串中,您得到的通知不需要進一步解析 String message = data.getString(「message」); ,你可以給你的應用程序的標題,如果沒有標題與您的應用程序

0

更改此代碼並嘗試它,我希望它的工作正常。 您最後一次錯過.Build();

來源:

NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) 
      // .setSmallIcon(R.mipmap.philips_launcher) 
      .setSmallIcon(getNotificationIcon()) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(sound) 
      .setColor(Color.parseColor("#0089C4")) 
      .setStyle(inboxStyle) 
      // .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setContentIntent(pendingIntent); 

要:

NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) 
      // .setSmallIcon(R.mipmap.philips_launcher) 
      .setSmallIcon(getNotificationIcon()) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(sound) 
      .setColor(Color.parseColor("#0089C4")) 
      .setStyle(inboxStyle) 
      // .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setContentIntent(pendingIntent).build(); 
+0

感謝您的回覆。讓我檢查並回復給您 – Anand

0

與通知消息的白色背景的問題是已知的,被固定,修復應該很快被釋放。另一種選擇是使用數據消息並在onMessageReceived回調中自行管理通知的創建。