我刚刚在android编程中迈出了第一步,我想尝试实现parse.com推送通知。我使用1.8.0版本。当我测试我有下一个错误:
1)错误:(20,68)错误:无法访问任务类文件中的bolt.Task not found。
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 -->
答案 0 :(得分:0)
1)您应该检查Eclipse项目的libs文件夹。可能你错过了最新的螺栓罐文件。这是包含解析lib文件的zip的一部分。
2)不幸的是,官方教程有些误导,因为它们没有完全更新API的最新更改。实际上这是必要的,直到Parse版本1.6,如果我没有弄错的话
现在,正如您在自定义接收器的代码中所看到的,您可以通过设置待定意图来设置单击通知时要打开的活动。请查看deprecation的此便笺。
使用新的ParsePushBroadcastReceiver,当前的实施比以往更灵活!因此,不要扩展BroadcastReceiver,而是扩展它。请务必阅读有关此文档。这非常有帮助。
此外:
a)在您的清单中,用以下
<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)在您的清单中,确保使用您创建的ParseApplication类作为name属性:
<application
android:name=".ParseApplication"
....
....
/>
我希望以上内容能帮助您继续完成项目。