2016-08-10 49 views
0

我在我的android應用程序中實現GCM服務,我也收到通知。但是當我的應用程序關閉或在後臺時,我們遇到問題。當android在後臺應用程序中時,通知圖標不可見(Gcm推送通知)?

當應用程序在前臺,那麼一切工作正常,我收到所有文本和圖標的通知,但當我的應用程序在後臺,我們得到通知文本和標題,但圖標不可見。我對此進行了搜索,並得出結論:當您的應用程序處於後臺時,通知由設備通知托盤處理。

這裏是我的代碼,以接收通知:

public class GCMPushReceiverService extends GcmListenerService { 

//This method will be called on every new message received 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    //Getting the message from the bundle 
    String message = data.getString("message"); 
    Log.d("data",data.toString()); 
    //Displaying a notiffication with the message 
    String body = null; 
    String title = null; 
    try{ 

     String data1 = data.toString(); 
     String json = (data1.split("notification=Bundle\\[")[1]).split("\\]")[0]; 

     body = (json.split("body\\=")[1]).split("\\,")[0]; 
     // title = (((json.split("body\\=")[1]).split("\\,")[1]).split("title\\=")[1]).split("\\,")[0]; 
     title = (((json.split("body\\=")[1]).split("vibrate")[0]).split("title=")[1]).split(",")[0]; 



     Log.d("json",json); 

     JSONObject notificationJSON = new JSONObject(json); 

     //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"); 

     Log.d("body",body); 
     Log.d("title",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 titles) { 
    Intent intent = new Intent(this, NavigationDrawerActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("firsttab","notify"); 
    int requestCode = 0; 
    int number = 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(titles) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(sound) 
      .setNumber(++number) 
      .setColor(Color.parseColor("#0089C4")) 
      // .setStyle(inboxStyle) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(titles)) 

      .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.notification_icon : R.drawable.not_icon; 
} 



} 

我的問題是如何處理的通知時,我的應用程序在後臺?以及如何在應用程序處於後臺時顯示通知圖標?當我點擊通知時,會打開launcherActivity,但我想打開一些其他活動。

+0

你有試過嗎? http://stackoverflow.com/a/29207365/3092341 – lal

+0

@lal:我試過這個,並使用透明圖標,但我認爲我的問題是,當我的應用程序在後臺時,我的GCMPushReceiverService不會被調用? – Anand

+0

你確定它沒有被叫? –

回答

1

基於此thread,當app在後臺時,需要服務器端進行操作。數據部分將自動保存在意圖中,併發送給包含內容過濾器中該操作的活動。在此related SO question中指出,通知消息根據下游消息請求的「通知」對象中傳遞的屬性自動生成通知,但在此情況下不調用onMessageReceived。檢查這個tutorial。在後臺時,應用程序會在通知托盤中收到通知有效內容,並且只在用戶點擊通知時處理數據有效內容。

發送郵件時也可以發送set the priority to high。它允許GCM服務在可能時喚醒睡眠設備,並打開與應用服務器的網絡連接。

您可以檢查這些相關鏈接:

希望這有助於!

0

它看起來像你正在發送通知消息,通知消息被傳遞到onMessageReceived回調,當應用程序在你看到的前景。但是,當應用程序處於前臺時,通知消息不會傳遞給onMessageReceived,則會使用消息的通知負載自動顯示通知。如果在用戶點擊通知時有附加數據有效載荷,則有效載荷將在已啓動意圖的附加內容中可用。

如果你想完全控制消息的處理方式,那麼你應該使用數據消息,這些消息總是被傳遞給onMessageReceived回調函數。

查看更多有關在這裏處理兩個types of FCM messages的不同方式。

注意: - 如果您使用Firebase控制檯發送消息,則此時它僅支持通知消息。即使您添加自定義數據,它仍然會被視爲客戶端的通知消息。 - 如果您使用REST API發送通知消息,則可以指定click_action字段以確定在用戶單擊通知時將啓動哪個活動。

+0

,謝謝你的回覆。我正在嘗試添加數據有效負載並返回給您 – Anand

0

如果您使用的流式細胞儀,在您的應用程序清單中添加此:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. --> 
<meta-data 
    android:name="com.google.firebase.messaging.default_notification_icon" 
    android:resource="@drawable/ic_stat_ic_notification" /> 
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming 
    notification message. --> 
<meta-data 
    android:name="com.google.firebase.messaging.default_notification_color" 
    android:resource="@color/colorAccent" /> 

只有這樣的自定義顏色&圖標會在通知中顯示,當應用程序是在後臺。