【问题标题】:Parse.com android notification - get data into BroadcastReceiverParse.com android 通知 - 将数据放入 BroadcastReceiver
【发布时间】:2015-04-08 11:53:24
【问题描述】:

我需要帮助实现 ParsePushBroadcastReceiver, 为了使用它来解析一个 JSON 对象,选择一个 Activity 来启动。

我试过了:

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

或者

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

但是没有成功...

我的应用类

public class AppApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("vrum");
        Parse.initialize(this, "...", "..");
        PushService.setDefaultPushCallback(this, HomeActivity.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();

    }

当我点击通知时,我会进入 HomeActivity...如果我删除该行也是一样

 PushService.setDefaultPushCallback(this, HomeActivity.class);

【问题讨论】:

  • 阅读此parse.com/tutorials/android-push-notifications。你可以有一个自定义类扩展ParseBroadcastReceiver
  • pastebin.com/Djsk4HCk 这是我的代码。你能告诉我哪里错了吗?当我单击通知时,我打开 HomeActivity... CustomParseBroadcastReceiver 方法 onReceive 未被调用..
  • 这不是必需的。 PushService.setDefaultPushCallback(this, HomeActivity.class);
  • 我在阅读更多文章后简化了清单代码,现在我有了这个。 pastebin.com/xvQUTEaY 如果我删除 PushService.setDefaultPushCallback(this, HomeActivity.class);我不会再收到通知了。并且没有调用 CustomParseBroadcastReceiver onReceive 方法。
  • 您需要在广播接收器中显示通知。 SOmehting 喜欢 if(intent.hasExtra("com.parse.Data")) { try { json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 然后显示通知,点击后您可以导航到不同的活动

标签: android parse-platform push-notification


【解决方案1】:
 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 snippet to give an idea
                Intent resultIntent = null;
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
resultIntent = new Intent(context, NotificationOpenActivity.class);
stackBuilder.addParentStack(NotificationOpenActivity.class);
stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


                // 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>

基本上,对于从您的问题中提取的清单编辑,只需编辑 android:name 属性。

希望这会有所帮助!

【讨论】:

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