我在我的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,但我想打開一些其他活動。
你有試過嗎? http://stackoverflow.com/a/29207365/3092341 – lal
@lal:我試過這個,並使用透明圖標,但我認爲我的問題是,當我的應用程序在後臺時,我的GCMPushReceiverService不會被調用? – Anand
你確定它沒有被叫? –