我只是讓我在android編程的第一步,我想嘗試實現parse.com推送通知。我使用1.8.0版本。當我測試我有下一個錯誤:Error〜parse.com推送通知android?
1)錯誤:(20,68)錯誤:無法訪問任務類文件的螺栓。找不到任務。
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 -->
@LuckyLuky您好!當然,在文檔中,我注意到他們建議將 setDefaultPushCallback替換爲ParsePushBroadcastReceiver。在這裏我發現了一些信息,但不知道如何正確使用它。你可以檢查它http://stackoverflow.com/questions/26154855/exception-when-opening-parse-push-notification/26180181#26180181。這是否意味着我需要使用Receiver.java而不是ParseReceiver.java?事實上,現在對我來說,這是一個很大的混亂,因爲我不知道如何處理PasreApplication.java以及在什麼地方放鑰匙。你能解釋我嗎? –
你自己的接收器類很好。只需將此類聲明更改爲此公共類ParseReceiver擴展ParsePushBroadcastReceiver並用此public void onPushOpen(Context ctx,Intent intent)替換onReceive()聲明(儘管onReceive()工作得很好)。關於ParseApplication.java,你是否得到了應用程序密鑰和客戶端密鑰的解析?如果是這樣,在Keys.java上應用它們,你就可以了。 [Here](http://pastebin.com/QwXRbi0D)我引用了整個AndroidManifest.xml,以方便您查看代碼中的差異。 –
我按你所說的做了。我使用公共類ParseReceiver擴展ParsePushBroadcastReceiver而不是我的舊的,然後將OnReceive替換爲onPush方法,並添加到Push開啓後打開MainActivity。我注意到您的系統中的Manifect中的.ParseReceiver重複。它可以嗎?!最後一個我想問的是ParseApplication.java中的這一行,我有他們的問題 ParseInstallation.getCurrentInstallation()。saveInBackground();我需要做什麼?!錯誤與以前相同:無法訪問任務類文件的螺栓。未找到任務。 –