当app在android中的后台时,通知图标不可见(Gcm推送通知)?

时间:2016-08-10 10:35:39

标签: android google-cloud-messaging gcmlistenerservice

我正在我的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,但我想打开一些其他活动。

3 个答案:

答案 0 :(得分:1)

基于此thread,当app在后台时,需要服务器端进行操作。数据部分将自动保存在intent中,并且发送到活动包含内容过滤器中的该操作。在此related SO question中声明,通知消息会根据"通知"中传递的属性自动生成通知。下游消息请求的对象,但在这种情况下不会调用void add(int *x, int length) { // use x[0], x[1], etc. } 。检查此tutorial。在后台,应用程序在通知托盘中接收通知有效负载,并仅在用户点击通知时处理数据有效负载。

发送邮件时,您还可以set the priority to high。它允许GCM服务在可能的情况下唤醒休眠设备并打开与应用服务器的网络连接。

您可以查看以下相关链接:

希望这有帮助!

答案 1 :(得分:0)

看起来您正在发送通知消息,当应用程序位于您看到的前台时,通知消息将传递到onMessageReceived回调。但是,当应用程序位于前台时,通知消息未传递给onMessageReceived,则消息的通知有效内容将用于自动显示通知。如果存在附带的数据有效负载,则当用户点击通知时,有效负载将在启动的意图的附加内容中可用。

如果您想完全控制如何处理消息,那么您应该使用数据消息,这些消息始终传递给onMessageReceived回调。

详细了解这里处理两个types of FCM messages的不同方式。

注意: - 如果您使用Firebase控制台发送消息,则此时仅支持通知消息。即使您添加自定义数据,它仍将被视为客户端上的通知消息。 - 如果您使用REST API发送通知消息,则可以指定click_action字段以确定在用户单击通知时将启动哪个活动。

答案 2 :(得分:0)

如果您使用的是FCM,请在您的应用中将其添加到Manifest:

<!-- 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" />

只有自定义颜色和当应用程序处于后台时,图标将显示在通知中。