Android - Push notification blank when not on top

2019-08-05 08:47发布

问题:

I'm having a small issue with android push notifications

If there are 3 Notifications and only one of these displays the title and the message. The one which is on the top of the bar. If anyone has any idea what might be the issue please let me know

Refer image on this link, this is how i am able to receive notifications http://postimg.org/image/3z4a21ssp/

public void createNotification(Context context, Bundle extras)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            //.setContentTitle(extras.getString("title"))
            //.setTicker(extras.getString("title"))
            .setContentIntent(contentIntent);

    String message = extras.getString("message");


    //Added Try catch in case the old code doesnt work !
    try{
        //chgpk: Added this code to get bigger notification window
        NotificationCompat.BigTextStyle inboxStyle = new NotificationCompat.BigTextStyle();
        inboxStyle.setBigContentTitle(extras.getString("title"));
        inboxStyle.bigText(message);        
        mBuilder.setStyle(inboxStyle);

    }catch(Exception ex){
        if (message != null) {
            mBuilder.setContentText(message);
        } else {
            mBuilder.setContentText("<missing message content>");
        }

        NotificationCompat.InboxStyle inboxStyleEx =
                new NotificationCompat.InboxStyle();
        inboxStyleEx.setBigContentTitle(extras.getString("title"));
        inboxStyleEx.addLine(message);
        mBuilder.setStyle(inboxStyleEx);
    }


    String msgcnt = extras.getString("msgcnt");     
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    MY_NOTIFICATION_ID++;
    //mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
    mNotificationManager.notify((String) appName, MY_NOTIFICATION_ID, mBuilder.build());


}

Also Code from DOT NET IS

 WebRequest tRequest;
        //tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest = WebRequest.Create(vANDR_PN_url);

        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        string postData =
            "collapse_key=score_update&"
            + "time_to_live=108"
            + "&delay_while_idle=1"
            + "&data.message=" + value
            + "&data.time=" + System.DateTime.Now.ToString()
            + "&data.title=" + contentTitle
            + "&data.redirect_to_page=" + vredirect_to_page
            + "&registration_id=" + deviceId + "";

        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();

        System.IO.File.AppendAllText(LogFilePath, Environment.NewLine + "sResponseFromServer >> " + sResponseFromServer);
        if (sResponseFromServer == "Error=InvalidRegistration") {
            mStatus = "error";
        }

        tReader.Close();
        dataStream.Close();
        tResponse.Close();

回答1:

I think that the problem is that you are setting texts for the bigStyle but not for the default (small) view.

Set all the texts:

// Small - default style
NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(context.getApplicationInfo().icon)
        .setWhen(System.currentTimeMillis())
        .setContentTitle(extras.getString("title"))
        .setTicker(extras.getString("title"))
        .setContentIntent(contentIntent);
// Big style
NotificationCompat.BigTextStyle inboxStyle = new NotificationCompat.BigTextStyle();
    inboxStyle.setBigContentTitle(extras.getString("title"));
    inboxStyle.bigText(message);        
    mBuilder.setStyle(inboxStyle);


回答2:

Add this to the notification builder :

 mBuilder.setContentTitle(extras.getString("title"))
         .setContentText(message);

The BigTextStyle is only displayed for the notification on the top. For the other notifications you must use setContentTitle and setContentText.