android - 更新推送通知 Android

标签 android

我正在 Android 上实现推送通知。当我想更新我的通知时,问题就来了。我想堆叠通知,这样如果通知可见,我只需更新它并执行此操作。

mNotifyBuilder.setContentText(currentText)
    .setNumber(++numMessages);

但是每次我收到通知时,++numMessages 在被求和之前都被设置回 0。如果屏幕上有通知,我需要它从它离开的地方总结。这是代码:

//Class is extending GcmListenerService
public class GCMPushReceiverService extends GcmListenerService {

    //variables for message 1,on receiving job applications
    String name;
    String lastname;
    String jobtypename;
    String kasualjobdatetimepostedat;
    String kasualjobcount;
    int numMessages;

    //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");
        String messageid = data.getString("messageid");

        if(messageid.compareTo("1")==0){
            name=data.getString("firstname");
            lastname=data.getString("lastname");
            jobtypename=data.getString("jobtype");
            kasualjobdatetimepostedat=data.getString("kasualjobdatetimepostedat");
        }
        else
        {
            kasualjobcount=data.getString("kasualjobcount");
        }
            //Displaying a notification with the message
        sendNotification(message, messageid);
    }

    //This method is generating a notification and displaying the notification
    private void sendNotification(String message,String messageid) {
        Intent intent = new Intent(this, Main_Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int requestCode = 0;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Log.d("notificationid", String.valueOf(messageid));
        if(messageid.compareTo("2")==0) {//messages to do with jobs around 2
            String messageionkasualjobscount="There are "+kasualjobcount+" new jobs around you";
            NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this);
            noBuilder.setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("KasualJobs2")
                    .setContentText(messageionkasualjobscount)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent).setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(2, noBuilder.build()); //messageid = ID of notification
        }else{//messages to with users applying for job 1

            String messageionkasualjobapplication=name+ " "+ lastname+" has applied for the "+jobtypename+" you posted on "+kasualjobdatetimepostedat;
            NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this);
            noBuilder.setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("KasualJobs1")
                    .setContentText(messageionkasualjobapplication)
                    .setAutoCancel(true).setNumber(++numMessages)
                    .setContentIntent(pendingIntent).setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1, noBuilder.build()); //messageid = ID of notification

        }
    }

}

最佳答案

通常,您不应期望您的字段值在多次调用 onMessageReceived 时保持不变,因为该服务可能已被操作系统终止以释放内存。

我建议将您的消息存储在数据库中。每当您收到一条新消息时,将其插入数据库,并在用户阅读时将其删除。然后就可以很方便的查询到有多少条未读消息了。

关于android - 更新推送通知 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39029982/

相关文章:

java - 我们怎样才能使标签布局自动隐藏?

java - 在 OKHttpClient sslSocketFactory 中使用来自 KeyChain 的 X509Certificate

Android 内存不足异常而 HTML 取消转义字符串

android - 糟糕的 ImageView 位图质量?

android - 设计器布局在自定义对话框中未正确显示

android - WebView透明度在开启硬件加速时不起作用

javascript - 跳过了 773 帧!应用程序可能在其主线程上做了太多工作。 [安卓网页浏览]

android - 适用于 Android NDK 的 Googletest

java - 如何从sqlite读取blob数据并显示它

android - SQLite Android 数据库光标窗口分配 2048 kb 失败