更新推送通知Android

时间:2016-08-19 01:55:09

标签: 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

        }
    }

}

2 个答案:

答案 0 :(得分:2)

通常,您不应期望您的字段值在多次调用http://mysite.dev/Template/mysite.dev/dashboard/index 时保持不变,因为该服务可能已被操作系统杀死以释放内存。

我建议将您的邮件存储在数据库中。每当收到新消息时,将其插入数据库,并在用户读取时将其删除。然后,您可以轻松查询有多少未读消息。

答案 1 :(得分:0)

我建议您使用FCM而不是GCM(因为GCM将不再使用)。 并且要更新通知,请使用sqlite数据库存储日期和时间的通知。时间。阅读时清除通知&也将其从数据库中删除。