如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()? [英] How to override onPushReceive() of ParsePushBroadcastReceiver?

查看:11
本文介绍了如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Parse.com 的推送通知服务.根据doc:

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

覆盖 onPushReceive 以触发silent"的后台操作推

override onPushReceive to trigger a background operation for "silent" pushes

我在这里找到了 onPushOpen() 的源代码,但现在我必须覆盖 onPushReceive() 来自定义声音和振动的行为.我不知道我应该在 onPushReceive() 中做什么,是否有任何示例代码可以帮助我弄清楚 onPushReceive() 内部的逻辑?谢谢.

I found the source code of onPushOpen() here, but now I have to override onPushReceive() to customize the behavior of sound and vibration. I don't know what I should do in onPushReceive(), is there any sample code that help me figure out the logic inside onPushReceive()? Thanks.

推荐答案

创建一个扩展 ParsePushBroadcastReceiver 的新类:

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>

更多信息:http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/

这篇关于如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆