Pitfalls you may encounter when using Android notifications

Pitfalls you may encounter when using Android notifications

I've recently encountered some issues with Android notifications, which I didn't know about before.

[[236632]]

First paste a piece of code

  1. /**
  2. * Create a notification bar management tool
  3. */
  4. NotificationManager notificationManager = (NotificationManager) mContext.getSystemService
  5. (Context.NOTIFICATION_SERVICE);
  6.  
  7. notificationManager.cancel(105);
  8.  
  9. Intent equipListPage = new Intent(mContext, CommonActivity.class);
  10. equipListPage.putExtra( "fragmentName" , EquipListFragment.class.getName());
  11. equipListPage.putExtra( "json" , JSON.toJSONString(list));
  12. PendingIntent pi = PendingIntent.getActivity(mContext, 0, equipListPage, null );
  13.  
  14. /**
  15. * Instantiate the notification bar constructor
  16. */
  17. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
  18. Notification notification = mBuilder
  19. .setAutoCancel( true )
  20. .setContentTitle( "test" )
  21. .setContentText( "Discover " + list.size ( ) + " devices around you " )
  22. .setContentIntent(pi)
  23. .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.max_ic_launcher))
  24. .setSmallIcon(R.drawable.max_ic_launcher)
  25. .setWhen(System.currentTimeMillis())
  26. .setDefaults(Notification.DEFAULT_SOUND)
  27. .setPriority(NotificationCompat.PRIORITY_MAX)
  28. .build();
  29. notifyId = ( int ) System.currentTimeMillis();
  30. notificationManager.notify(105, notification);

The purpose is to notify users of something they have found around them, and then the user clicks on it to display a list. I quickly wrote the code and tested it. Then I released the version, but users kept saying that the list they clicked on was the same every time, which puzzled me. I thought it was not my problem, but I tried it myself. It was embarrassing. Sure enough, there was a problem, that is, the transmitted data was not updated.

How to solve

The problem is this sentence

  1. PendingIntent.getActivity(mContext, 0, equipListPage, null );

There are four parameters in total. See the explanation of the source code.

  1. * @param context The Context in which this PendingIntent should start
  2. * the activity.
  3. * @param requestCode Private request code for the sender
  4. * @param intent Intent of the activity to be launched.
  5. * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
  6. * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},

There are four FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT

I used FLAG_UPDATE_CURRENT to solve the problem. It is mainly used to update messages. For example, if you send a notification message with "123", and before clicking it, another notification message is sent with "345", then you click on both messages and get "345". So my problem was naturally solved.

Question 2

Later, there is another requirement to add a notification message to display different applications. That is, the message above, notification 1 needs to get "123", and notification 2 needs to get "456". What should we do? This requires the use of the second flag. When using FLAG_CANCEL_CURRENT, the above steps are still used. At this time, you will find that when you click on message 1, there is no response, but the second one can be clicked. The reason lies in the second parameter. You need to define a different requestCode for each different message, and the problem can be solved.

<<:  How to get "Apple Recommended" for a good game? There is a routine

>>:  2-Minute Coding Tip: Don't Use Loops in Your Code

Recommend

How to finish a month's worth of articles in just one hour? Only 6 steps!

The source of content has always been the most tr...

Daytime Research Society C4D Creative Design Course Gray Day - C4D IP Character Binding

File Directory: ├──Information | ├──MAC decompres...

What does it mean for WeChat to open 11 interfaces?

WeChat has opened 11 JS-SDK interfaces, including...

How to find user growth points? Build a Mini Program User Growth Model

1. If a business is not growing, it is dying I ha...

Silver among silver medals, also a champion

The symbol Ag for silver comes from its Latin nam...

Breaking through the limits, electric vehicles accelerate again

With the development of the past year, the world ...

Example analysis: How to write a product data report?

Product data reporting is an essential task for p...

Pinduoduo’s bargaining logic and strategic marketing methods!

Many people often receive price-cutting links fro...

Why is barrage so hated?

What are the reasons why barrage is hated? There ...