Android GCM and multiple tokens

后端 未结 5 1850
失恋的感觉
失恋的感觉 2020-12-03 21:36

I register in GCM with GoogleCloudMessaging.getInstance(context); and save received token on device. Then send it to server and it\'s associated with user account. If I uni

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 21:40

    first when you send notification send user id with it and ask if id in sharedpreference == comming or no

    if you send notifications to all users and may be some one get 2 notifications while he should only get one do that

    Create file on your server and with any number say 0 then when you want to send notification send this numebr with it then add one to this number++ to be new number in the next notification same with every new one

    In android application add variable and let this variable = the comming variable from the server after add the notification but you need to ask if(number_in_your_service!=server_number) //add notification

    any number of notifications you send just one will appear

    public class GcmIntentService extends IntentService {
      public static int openintent;
      public static final int NOTIFICATION_ID = 1;
      private static final String TAG = "GcmIntentService";
    
      private static String number_in_your_service="somethingneversend";
      NotificationCompat.Builder builder;
    
      public GcmIntentService() {
        super("GcmIntentService");
      }
    
      @Override
      protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
    
        if (!extras.isEmpty()) { // has effect of unparcelling Bundle
          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
          } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            // If it's a regular GCM message, do some work.
          } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            for (int i = 0; i < 5; i++) {
              Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
              }
            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
            // Post notification of received message.
            sendNotification(extras);
            Log.i(TAG, "Received: " + extras.toString());
          }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
      }
    
      private void sendNotification(Bundle extras) {
    
        if((extras.getString("server_number")).equals(number_in_your_service)) {
    
          Intent intent = new Intent(this, Main_Page_G.class);
          intent.putExtra("frame",100);
          intent.putExtra("bundle",extras);
          final PendingIntent contentIntent = PendingIntent.getActivity(this,
                120, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          NotificationManager mNotificationManager;
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                GcmIntentService.this).setContentTitle("name")
                .setContentText("content")
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentInfo("Test")
                .setSmallIcon(R.drawable.rehablogo2)
                .setAutoCancel(true);
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager = (NotificationManager) GcmIntentService.this
                .getSystemService(Context.NOTIFICATION_SERVICE);
          mNotificationManager.notify(id, mBuilder.build());
          id=Integer.parseInt(extras.getString("id"));
        }
      }
    }
    

提交回复
热议问题