Android: update specific notification from multiple notifications which have remoteview

岁酱吖の 提交于 2019-12-01 21:16:56

I would just add an ID in the intent that you put into the PendingIntent to know which notification you are operating on, so something like this:

Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);

intent.putExtra(EXTRA_ID, songID);

You can pull this extra out later in the code getting called:

String songID = intent.getStringExtra(EXTRA_ID);

After lot of try i figured out the solution by changing some code in pending intent, as shown in below code

my code is below

public void UpdateNotificationfunction(int currentNotificationNo) {
 RemoteViews mRemoteViews1 = null;
    PendingIntent pendingIntent = null;
    PendingIntent pendingIntent1 = null;
    PendingIntent pendingIntent2 = null;
    PendingIntent pendingIntent3 = null;
    PendingIntent pendingIntent4 = null;
    PendingIntent pendingIntent5 = null;

    if (mRemoteViews1 == null) {
        Log.d("LOG", "mRemoteViews is null");

            mRemoteViews1 = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);

    } else { 
        if (mState == palyer.Paused || mState == palyer.Stopped) { 

            try { 
                Log.e("LOG", "State.Paused || State.Stopped");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti);  // play icon

            } catch (Exception e) {}

        } else if (mState == palyer.Playing) { 
            try { 
                Log.e("LOG", "State.Playing");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        } else if (mState == palyer.Retrieving) { 
            try { 
                Log.e("LOG", "else Retrieving");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.playdetailfornoti); // play icon

            } catch (Exception e) {}
        } 
        else { 
            try { 
                Log.e("LOG", "else");
                mRemoteViews1.setImageViewResource(R.id.playpush,
                        R.drawable.pushdetail);   // pushicon

            } catch (Exception e) {}
        } 
    } 

    Intent intent = new Intent(ACTION_TOGGLE_PLAYBACK);
    Intent intent1 = new Intent(CLOSE_PUSH_NOTIFICATION);
    Intent intent2 = new Intent(ACTION_NEXT);
    Intent intent3 = new Intent(ACTION_PREVIOUS);
    Intent intent4 = new Intent(ACTION_STOP);
    Intent intent5 = new Intent(ACTION_PLAY_NOTIFICATION_START);

    Intent newintent = new Intent(this, Splace.class);
    newintent.putExtra("newsId",_id);
    newintent.putExtra("message",title);
    newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    newintent.setAction("actionstring" + System.currentTimeMillis());

    pendingIntent = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent, PendingIntent.FLAG_UPDATE_CURRENT); // changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent1 = PendingIntent.getService(getApplicationContext(), 0,
            intent1, 0);
    pendingIntent2 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent2, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent3 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent3, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            newintent, 0);
    pendingIntent4 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent4, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP
    pendingIntent5 = PendingIntent.getService(getApplicationContext(),
            currentNotificationNo, intent5, PendingIntent.FLAG_UPDATE_CURRENT);// changed here currentNotificationNo insted of REQUEST_CODE_STOP


    mRemoteViews1.setTextViewText(R.id.playertitle,
            MusicList.CurrentNotificationEntity.scrape_title); 

    mRemoteViews1
                .setTextViewText(R.id.playerapaer, Base
                        .getNewsPaperName(Base.sharedPref.getString( 
                                MusicList.CurrentNotificationEntity.newspaperID, 
                                "Palpal"))); 
    mRemoteViews1.setOnClickPendingIntent(R.id.forward, pendingIntent2);
    mRemoteViews1.setOnClickPendingIntent(R.id.backword, pendingIntent3);

    mRemoteViews1.setOnClickPendingIntent(R.id.playertitle, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerapaer, contentIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.playerimage, contentIntent);

    mRemoteViews1.setOnClickPendingIntent(R.id.playpush, pendingIntent);
    mRemoteViews1.setOnClickPendingIntent(R.id.close, pendingIntent1);

    Notification  mNotification1 = new NotificationCompat.Builder(
                getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_HIGH)
                .setDeleteIntent(pendingIntent1)
                .setAutoCancel(true) 
                .setContent(mRemoteViews1).setOngoing(false).build();

    mNotificationManager.notify(currentNotificationNo, mNotification1); // changed here currentNotificationNo insted of Base.currentNotificationNo
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!