我刚刚迈出了 Android 编程的第一步,我想尝试实现 parse.com 推送通知。我用的是1.8.0版本。当我测试时,我遇到了下一个错误:
1) 错误:(20, 68) 错误:无法访问 bolt 的任务类文件。未找到任务。
2) setDefaultPushCallback 已弃用。
在下面您可以看到我使用的代码。我错过了什么以及我需要改变什么?!也许有人有很好的例子?
ParseReceiver.java
public class ParseReceiver extends BroadcastReceiver {
private final String TAG = "Parse Notification";
private String msg = "";
@Override
public void onReceive(Context ctx, Intent intent) {
Log.i(TAG, "PUSH RECEIVED!!!");
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
if(key.equals("string")){
msg = json.getString(key);
}
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.happy);
Intent launchActivity = new Intent(ctx, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(ctx, 0, launchActivity, 0);
Notification notification = new NotificationCompat.Builder(ctx)
.setContentTitle("PUSH RECEIVED")
.setContentText(msg)
.setSmallIcon(R.drawable.happy)
.setLargeIcon(icon)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notification_manager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notification_manager.notify(0, notification);
}
}
ParseApplication.java
public class ParseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, Keys.applicationId, Keys.clientKey);
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Keys.java
public class Keys {
protected static final String applicationId = "";
protected static final String clientKey = "";
}
在 Manifect 文件中我使用了下一个代码
<!-- Permissions required for parse.com notifications -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- END Parse permissions -->
<!-- My custom receiver -->
<receiver android:name=".ParseReceiver" >
<intent-filter>
<action android:name="com.makemyandroidapp.parsenotificationexample.RECEIVE_PUSH" />
</intent-filter>
</receiver>
<!-- END my custom receiver -->
<!-- Required for Parse.com notifications -->
<service android:name="com.parse.PushService" />
<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>
<!-- END Parse.com requirements -->
最佳答案
1) 您应该检查 Eclipse 项目的 libs 文件夹。您可能缺少最新的 Bolts jar 文件。这是包含解析 lib 文件的 zip 的重要组成部分。
2) 不幸的是,官方教程有些误导,因为它们没有完全更新 API 的最新更改。实际上,如果我没记错的话,在 Parse 1.6 版本之前这是必要的。
现在,正如您在自定义接收器的代码中所看到的,您可以通过设置待处理 Intent 来设置单击通知时将打开的 Activity 。查看deprecation的注释。
当前的实现,使用新的ParsePushBroadcastReceiver ,比以往更加灵活!因此,与其扩展 BroadcastReceiver,不如扩展这个。请务必阅读有关此内容的文档。这非常有帮助。
还有:
a) 在您的 list 中,将“我的自定义接收器”部分替换为以下内容
<receiver android:name=".ParseReceiver" android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.OPEN" />
<action android:name="com.parse.push.intent.DELETE" />
</intent-filter>
</receiver>
b) 更改此设置
<receiver android:name="com.parse.ParseBroadcastReceiver" >
发送至您自己的自定义接收器
<receiver android:name="com.makemyandroidapp.parsenotificationexample.ParseReceiver">
c) 在 list 中,确保使用您创建的 ParseApplication 类作为名称属性:
<application
android:name=".ParseApplication"
....
....
/>
我希望上述内容能够帮助您继续您的项目。
关于java - 错误〜parse.com推送通知android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711223/