自定义通过解析收到的通知

时间:2014-11-30 16:57:46

标签: android parse-platform google-cloud-messaging android-notifications

我想捕获从解析发送的通知,然后从解析发出的json数据中创建通知。我尝试使用另一个SO答案更改默认解析的GCM接收器,但仍然只显示默认通知。

我的清单:         

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.mypackage.Notification"
        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" />

             <!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
            <category android:name="com.mypackage" />
        </intent-filter>
    </receiver>

我的自定义接收器类:

public class Notification extends BroadcastReceiver {

    private static final String TAG = "MyCustomReceiver";
    private static final int NOTIFICATION_ID = 1;
    public static int numMessages = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        try {
            String action = intent.getAction();
             String channel = intent.getExtras().getString("com.parse.Channel");
             JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
             Log.i("mari ñapas", "got action " + action + " on channel " + channel + " with:");

             if (action.equalsIgnoreCase("my.application.technoat.NEW_NOTIF")) {
                 String title = "title";
                 if (json.has("header"))
                      title = json.getString("header");
                  generateNotification(context, title, json);
             }
           } catch (JSONException e) {
             Log.d(TAG, "JSONException: " + e.getMessage());
           }
    }

    private void generateNotification(Context context, String title, JSONObject json) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

        NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_drawer)
        .setContentTitle(title)
        .setContentText("New Post in the Blog")
         .setNumber(++numMessages);

        mBuilder.setContentIntent(contentIntent);

        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());

        }

    }

我仍然可以收到通知,但它不是由我的自定义类生成的,而是由默认的解析通知处理程序生成的。有人能指出这个问题吗?

3 个答案:

答案 0 :(得分:2)

不幸的是,这里的答案有点陈旧。它过去是不可能&#34;拦截&#34; Parse创建通知的方式,因此您必须创建自定义JSON blob,它通常不会创建通知,而是广播意图,听取该意图,然后创建自己的通知。这是Android柔道,我们努力使better API考虑​​到可定制性。

简而言之:遵循更新的教程,这将要求您注册新的com.parse.ParsePushBroadcastReceiver。要自定义行为,请注册您自己的ParsePushBroadcastReceiver子类,并将方法重载到您心中。如果您想通过通知呈现获得真正的创意,只需覆盖getNotification

答案 1 :(得分:1)

这些代码是从许多SO答案中分出的,所以我可以将它们全部链接起来。感谢所有原作者。

这是我的第一个答案。因此,如果无论如何我可以改进它,请建议我。

所以我通过捕获通知,分阶段读取json标记并构建通知来使其工作。

广播接收器类:

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        final Dbhelper p = new Dbhelper(context);
        try {
            String action = intent.getAction();
             String channel = intent.getExtras().getString("com.parse.Channel");
             JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
             Log.i("Got push", "got action " + action + " on channel " + channel + " with:");

             if (action.equalsIgnoreCase("My.app.package")) {
                 String title = "title";
                 String message = "";
                 if (json.has("header"))
                     title = json.getString("header");
                 if (json.has("msg"))
                     message = json.getString("msg");
                    numMessages++;
                    p.addCircular(title, message);
                  generateNotification(context, title, message, json);
             }
           } catch (JSONException e) {
             Log.d(TAG, "JSONException: " + e.getMessage());
           }
    }

    private void generateNotification(Context context, String title, String message, JSONObject json) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

        final NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_drawer)
        .setContentTitle(title)
        .setContentText(message)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .addAction(0, "Press to open", contentIntent)
        .setAutoCancel(true)
        .setDefaults(new NotificationCompat().DEFAULT_ALL);
        //.setNumber(++numMessages);

        mBuilder.setContentIntent(contentIntent);

        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());

        }

    }

将清单中的解析GCM接收器更改为

 <receiver
            android:name="My.app.package.MyBroadcastReciverClass"
            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" />
                <action android:name="com.sbu.sathyabama" />
                 <!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
                <category android:name="My.app.package" />
            </intent-filter>
 </receiver>

要发送的json数据编码为

{
     "header": "My notification Title",
     "msg": "My Notification message",
     "action": "my.app.package"
}

答案 2 :(得分:0)

如果您使用Parse发送通知。为什么你不使用ParsePush

进行解析推送的示例代码:

JSONObject data = new JSONObject("{\"alert\": \"The Mets scored!\",
                                   \"badge\": \"Increment\",
                                   \"sound\": \"cheering.caf\"}");

ParsePush push = new ParsePush();
push.setChannel("Mets");
push.setData(data);
push.sendPushInBackground();