ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在点击通知之前推送Parse打开通知

2019-08-30 22:25:27  阅读:124  来源: 互联网

标签:parse-platform android push-notification


这就是我的设置看起来的样子.

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上控制通知创建最好.如果您没有推送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());
}

标签:parse-platform,android,push-notification
来源: https://codeday.me/bug/20190830/1771030.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有