在Android应用中检测推送通知的到来

时间:2015-04-09 12:46:40

标签: android parse-platform push-notification

是否可以在Android应用程序上以编程方式检测推送通知的到来?如何继续实施相同的目标?

1 个答案:

答案 0 :(得分:2)

正如@drees在评论中建议的那样,你可以创建一个扩展ParsePushBroadcastReceiver的自定义广播接收器。

像这样:

public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {

// Sample code
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            final String notificationTitle = json.getString("title").toString();
            final String notificationContent = json.getString("alert").toString();
            final String uri = json.getString("uri");

//Create a taskstack builder - this is just sample(incomplete) to give an idea
            Intent resultIntent = null;
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

            // Customize your notification
            NotificationCompat.Builder builder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.mipmap.ic_notification_icon)
                            .setContentTitle(notificationTitle)
                            .setContentText(notificationContent)
                            .setGroup(GROUP_SHORTR_NOTIFS)
                            .setContentIntent(resultPendingIntent)
                            .setAutoCancel(true)
                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                            .setDefaults(Notification.DEFAULT_VIBRATE)
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(notificationContent));

            int mNotificationId = 001;
            NotificationManager mNotifyMgr =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, builder.build());


        } catch (JSONException e) {
            Log.d(TAG, e.getMessage());
        }

    }
}

在清单中添加以下内容。

 <receiver
            android:name=".receivers.ParseCustomBroadcastReceiver"
            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>

如果您关注this tutorial,则上述清单编辑只需要您更改android:name属性。

希望这有帮助。