繁体   English   中英

当应用程序在android中处于后台时,通知图标不可见(Gcm推送通知)?

[英]Notification Icon not visible (Gcm Push notification) when app is in background in android?

我在我的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。

基于此线程 ,当应用程序在后台运行时,它需要服务器端执行操作。 数据部分将自动保存为一个意图,并在内容过滤器中发送给包含该动作的活动。 在此相关的SO问题中指出,通知消息会根据在下游消息请求的“通知”对象中传递的属性自动生成通知,但是在这种情况下不会调用onMessageReceived 检查本教程 在后台时,应用程序会在通知托盘中接收通知有效载荷,并且仅在用户点击通知时处理数据有效载荷。

您还可以在发送消息时将优先级设置为高 它允许GCM服务在可能的情况下唤醒睡眠设备并打开与您的应用服务器的网络连接。

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

希望这可以帮助!

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

如果要完全控制消息的处理方式,则应使用数据消息,该消息始终传递到onMessageReceived回调中。

此处详细介绍了处理两种FCM消息的不同方式。

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

如果您使用的是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" />

只有当应用程序在后台运行时,自定义颜色和图标才会显示在通知中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM