簡體   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