Ich implementiere GCM-Service in meiner Android-App und ich bekomme Benachrichtigung auch. Aber wir bekommen Problem, wenn meine App geschlossen ist oder im Hintergrund.Benachrichtigungssymbol nicht sichtbar (Gcm Push-Benachrichtigung) wenn App im Hintergrund in Android ist?
Wenn App im Vordergrund ist dann funktioniert alles gut, ich bekomme eine Benachrichtigung mit allen Text und Symbol, aber wenn meine App im Hintergrund ist, erhalten wir Benachrichtigungstext und Titel, aber Symbol ist nicht sichtbar. Ich habe danach gesucht und bin zu dem Schluss gekommen, dass die Benachrichtigung über das Gerätebenachrichtigungsfach erfolgt, wenn sich Ihre App im Hintergrund befindet.
Hier ist mein Code Benachrichtigung zu erhalten:
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;
}
}
Meine Frage ist, wie Benachrichtigung behandelt, wenn meine Anwendung im Hintergrund ist? Und wie wird das Benachrichtigungssymbol angezeigt, wenn sich die App im Hintergrund befindet? Wenn ich auf Benachrichtigung geklickt habe, öffne ich launcherActivity, aber ich möchte etwas anderesActivity öffnen.
haben Sie das versucht? http://Stackoverflow.com/a/29207365/3092341 – lal
@lal: Ich versuchte dies und mit transparenten Symbol, aber ich denke, mein Problem ist, dass mein GCMPushReceiverService nicht aufgerufen wird, wenn meine App im Hintergrund ist? – Anand
Sind Sie sicher, dass es nicht heißt? –