【问题标题】:Notification Icon not visible (Gcm Push notification) when app is in background in android?当应用程序在 android 的后台时,通知图标不可见(Gcm 推送通知)?
【发布时间】:2016-08-10 10:35:39
【问题描述】:

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

【问题讨论】:

  • @lal:我试过这个并使用透明图标,但我认为我的问题是我的 GCMPushReceiverService 在我的应用程序处于后台时没有被调用?
  • 确定没有被调用?

标签: android google-cloud-messaging gcmlistenerservice


【解决方案1】:

基于这个thread,当app在后台时,它需要服务器端来做一个动作。数据部分将自动保存在一个意图中,并发送到内容过滤器中包含该操作的活动。在这个related SO question 中声明,通知消息会根据您的下游消息请求的“通知”对象中传递的属性自动生成通知,但是在这种情况下不会调用onMessageReceived。检查此tutorial。在后台时,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载。

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

您可以查看这些相关链接:

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    看起来您正在发送通知消息,当应用程序在您看到的前台时,通知消息将传递到 onMessageReceived 回调。但是,当应用程序处于前台时,通知消息不会传递给 onMessageReceived,消息的通知负载用于自动显示通知。如果有一个伴随的数据有效负载,当用户点击通知时,该有效负载将在已启动 Intent 的附加内容中可用。

    如果您想完全控制消息的处理方式,那么您应该使用数据消息,这些消息总是传递给 onMessageReceived 回调。

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

    注意: - 如果您使用 Firebase 控制台发送消息,它目前仅支持通知消息。即使您添加了自定义数据,它仍然会在客户端被视为通知消息。 - 如果您使用 REST API 发送通知消息,您可以指定 click_action 字段来确定当用户点击通知时将启动哪个 Activity。

    【讨论】:

    • ,感谢您的回复。我正在尝试添加数据有效负载并返回给您
    【解决方案3】:

    如果您使用 FCM,请将其添加到您的应用清单中:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多