【问题标题】:Push notification with Parse opening before clicking on notification在单击通知之前使用 Parse 打开推送通知
【发布时间】:2015-05-02 00:59:19
【问题描述】:

这就是我的设置的样子。

LunchActivity 有代码:

Parse.initialize(this, "MY_APP_ID", "MY_APP_KEY");
PushService.subscribe(this, "MyCity", HomeActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();

HomeActivity 类是一个简单的活动类,它默认打开一个简单的屏幕。我还写了一个自定义接收器。

public class CityPushReceiver extends BroadcastReceiver {
    private static final String TAG = "CityPushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            JSONObject json = new JSONObject(intent.getExtras().getString(
                    "com.parse.Data"));

            Integer event_id = Integer.parseInt((String) json.get("event_id"));

            Intent eventIntent = new Intent(context, EventResult.class);
            eventIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            eventIntent.putExtra("event_id", event_id);
            context.getApplicationContext().startActivity(eventIntent);

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

清单文件有条目:

<receiver
    android:name="com.myapp.CityPushReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="com.myapp.CITY_NOTIFICATION" />
    </intent-filter>
</receiver>

我使用 Python 代码推送通知:

import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps({
       "channels": [
         "MyCity"
       ],
       "data": {
     "action": "com.myapp.CITY_NOTIFICATION",
         "alert": "New Event Notification",
     "event_id": "425"
       }
     }), {
       "X-Parse-Application-Id": "APP_ID",
       "X-Parse-REST-API-Key": "API_KEY",
       "Content-Type": "application/json"
     })
result = json.loads(connection.getresponse().read())
print result

此设置未按预期工作。我确实在我的设备上收到了通知(我正在使用 AVD 进行测试)。但即使我没有点击托盘中的通知,它也会打开预期的EventResult 活动。即使我在设备主屏幕上并且应用程序仅在后台运行,也会发生这种情况。当我点击托盘中的通知时,它会打开定义为默认类的HomeActivity 类。

仅当我单击托盘中的通知时,预期的行为是打开EventResult。你们能告诉我需要改变什么吗?

【问题讨论】:

    标签: android push-notification parse-platform


    【解决方案1】:

    我发现在 Android 上最好控制通知的创建。如果您没有在推送中send a title or alert field,则 parse.com android SDK 不会创建通知,您可以在推送时自己创建通知。

    这是我用来创建通知的方法:

    void CreateNotication(Context context, String title, String pushId) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MyClass.class), 0);
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(title)
                .setContentIntent(contentIntent);
    
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(pushId, 1, mBuilder.build());
    }
    

    【讨论】:

    • 唯一的问题是扩展 ParsePushBroadcastReceiver 的接收器不会跟踪 onPushOpen() 当应用程序在后台点击推送通知并且用户点击 NotificationManager 发送的通知时
    【解决方案2】:

    这样使用:

    public class Application extends android.app.Application {
    
        public Application() {
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
    
            PushService.setDefaultPushCallback(this, MainActivity.class);
        }
    }
    

    //MainActivity.java - 点击通知时需要打开的Activity。

    在您的清单文件中添加此应用程序。

    <application
        android:label="@string/app_name"
        android:name="com.parse.tutorials.pushnotifications.Application"
        android:theme="@style/AppTheme">
    
        <activity
            android:label="@string/app_name"
            android:name="com.parse.tutorials.pushnotifications.MainActivity">
    
            <intent-filter>
    
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    并将这一行添加到您需要在单击通知时打开的 Activity 类中。

    ParseAnalytics.trackAppOpened(getIntent()); 
    

    检查这个答案:LINK

    【讨论】:

    • 这是否意味着我只能在发送推送通知时打开 HomeActivity?或者它只是一个默认活动?如果我想根据不同的标准发送不同的活动怎么办?另外,我会尝试这个解决方案并分配赏金。
    • 我尝试了您通过简单通知提供给我的代码(来自仪表板,而不是来自 python 脚本),它仅在应用程序处于活动状态或在后台运行时才有效。我从托盘中删除了该应用程序,它停止接收通知。当我重新打开应用程序时,我收到了所有待处理的通知。
    【解决方案3】:

    坦率地说,这里发布的所有答案在一种或另一种情况下都是正确的,所以我会在这里正确看待事情。

    关于我的具体问题,正确的看待它的方法是,如果您实现自己的广播接收器,则每当您收到推送时都会调用它,并且其中的任何代码都会被执行。为确保您看到通知,有两个重要事项:

    1. 确保在发送推送时使用警报
    2. 使用 Parse 提供的默认接收器。即在配置中只告诉 parse 要调用哪个活动,发送带有警报的推送,然后让 parse 处理其余的东西。它将显示警报,并在您单击警报时打开特定活动。另一种方法更复杂,那就是编写您自己的接收器,并将所有注意事项纳入警报。

    很大程度上也取决于您发送通知的方式。因此,请确保正确提及警报、数据,有时甚至是活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多