How to override onPushReceive() of ParsePushBroadcastReceiver?

后端 未结 2 1342
傲寒
傲寒 2020-12-29 16:18

I am using push notification service of Parse.com. According to the doc:

override onPushReceive to trigger a background operation for \"silent\" pus

相关标签:
2条回答
  • 2020-12-29 16:34

    You can use the intent extra parameter "action" to call your intent to handle whatever you want.

    Original onPushReceive source:

    protected void onPushReceive(Context context, Intent intent) {
        JSONObject pushData = null;
    
        try {
            pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
        } catch (JSONException var7) {
            Parse.logE("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var7);
        }
    
        String action = null;
        if(pushData != null) {
            action = pushData.optString("action", (String)null);
        }
    
        if(action != null) {
            Bundle notification = intent.getExtras();
            Intent broadcastIntent = new Intent();
            broadcastIntent.putExtras(notification);
            broadcastIntent.setAction(action);
            broadcastIntent.setPackage(context.getPackageName());
            context.sendBroadcast(broadcastIntent);
        }
    
        Notification notification1 = this.getNotification(context, intent);
        if(notification1 != null) {
            ParseNotificationManager.getInstance().showNotification(context, notification1);
        }
    
    }
    

    And no notification if no "alert" or "title" extra in the intent.

    So you do not need to extend any class at all for silent push updates...

    0 讨论(0)
  • 2020-12-29 16:46

    Create a new class that extends ParsePushBroadcastReceiver:

    public class MyPushBroadcastReceiver extends ParsePushBroadcastReceiver {
    
    public static final String PARSE_DATA_KEY = "com.parse.Data";
    
       @Override
       protected Notification getNotification(Context context, Intent intent) {
          // deactivate standard notification
          return null;
       }
    
       @Override
       protected void onPushOpen(Context context, Intent intent) {
          // Implement       
       }  
    
       @Override
       protected void onPushReceive(Context context, Intent intent) {
          JSONObject data = getDataFromIntent(intent);
          // Do something with the data. To create a notification do:
    
          NotificationManager notificationManager =
          (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
          NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
          builder.setContentTitle("Title");
          builder.setContentText("Text");
          builder.setSmallIcon(R.drawable.ic_notification);
          builder.setAutoCancel(true);
    
          // OPTIONAL create soundUri and set sound:
          builder.setSound(soundUri);
    
          notificationManager.notify("MyTag", 0, builder.build());
    
       }
    
       private JSONObject getDataFromIntent(Intent intent) {
          JSONObject data = null;
          try {
             data = new JSONObject(intent.getExtras().getString(PARSE_DATA_KEY));
          } catch (JSONException e) {
             // Json was not readable...
          }
          return data;
       }
    }
    

    Add this in your Manifest:

      <receiver
         android:name=".MyPushBroadcastReceiver"
         android:exported="false">
         <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
         </intent-filter>
      </receiver>
    

    Further information: http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/

    0 讨论(0)
提交回复
热议问题