parse.com自定义通知接收器

时间:2015-07-15 18:01:08

标签: android parse-platform push-notification

我正在使用自定义通知接收器类来进行parse.com推送通知来改变声音,振动......有时候,它看起来通知没有到达。我可以在解析网站上看到推送通知发送。也许有人可以查看我的代码并看到一些错误的代码片段。

清单:

  <service android:name="com.parse.PushService" />

  <receiver
      android:name="com.parse.GcmBroadcastReceiver"
      android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" />


          <category android:name="com.mflapp.test" />
      </intent-filter>
  </receiver>
  <receiver
      android:name=".MyCustomReceiver"
      android:exported="false" >
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <action android:name="android.intent.action.USER_PRESENT" />
          <action android:name="com.parse.push.intent.RECEIVE" />
          <action android:name="com.parse.push.intent.OPEN" />
          <action android:name="com.parse.push.intent.DELETE" />
      </intent-filter>
  </receiver>

MyCustomReceiver

public class MyCustomReceiver  extends ParsePushBroadcastReceiver {
    private static int NOTIFICATION_ID = 1;


    protected void onPushReceive(Context mContext, Intent intent) {

        try {
            String action = intent.getAction();
            String channel = intent.getExtras().getString("com.parse.Channel");
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            if (action.equalsIgnoreCase("com.parse.push.intent.RECEIVE")) {
                NOTIFICATION_ID++;
                String title = mContext.getResources().getString(R.string.app_name);
                if (json.has("alert"))
                {
                    String text = json.getString("alert");
                    generateNotification(mContext, title, json, text);
                }
            }
        } catch (JSONException e) {
        }
    }


    private void generateNotification(Context context, String title, JSONObject json, String text) {
        Intent intent = new Intent(context, home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setContentText(text)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
                        .setTicker(text)
                        .setLights(Color.GREEN, 500, 500);
        mBuilder.setContentIntent(contentIntent);
        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
    }

}

1 个答案:

答案 0 :(得分:1)

你真的错过了@Override音符吗? 该方法的签名应为:

@Override 
protected void onPushReceive(Context mContext, Intent intent) 

当我遇到通知问题时,我添加了一行: 机器人:名称=&#34; .MyApplication&#34;到Android清单中的应用程序标签,它帮助了我。

另外,我不确定你的数字系统是好的。当您重新启动应用时,计数再次从1开始。我假设你在为用户展示时使用了通知ID,现在它没有用。