【问题标题】:parse.com custom notification receiverparse.com 自定义通知接收器
【发布时间】:2016-02-16 18:36:16
【问题描述】:

我正在使用 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());
    }

}

【问题讨论】:

  • 没人知道吗?谢谢

标签: android parse-platform push-notification


【解决方案1】:

你就没有遗漏@Override 注释吗? 方法的签名应该是:

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

当我遇到通知问题时,我添加了一行: android:name=".MyApplication" 到 Android 清单中的应用程序标记,它帮助了我。

我也不确定你的数字系统是否良好。当您重新启动应用程序时,计数再次从 1 开始。我想您在向用户显示通知 id 时正在使用它,但现在它不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多