简体   繁体   中英

Error ~ parse.com push notification android?

I just make my first steps in android programming and I wanna try to realise parse.com push notification. I use 1.8.0 version. When i tested i had next errors:

1) Error:(20, 68) error: cannot access Task class file for bolts.Task not found.

2) setDefaultPushCallback is deprecated.

Here below you can see code that i used. What i missed and what i need to change?! Maybe someone have good example?

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 = "";
}

In Manifect file i used next code

<!-- 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) You should check your Eclipse project's libs folder. Possibly you are missing the latest bolts jar file. This is part and parcel of the zip that contains the parse lib files.

2) Unfortunately, the official tutorials are somewhat misleading because they are not fully updated with the latest changes of the API. Actually this was necessary until Parse version 1.6, if I am not mistaken.
Now, as you can see in your custom receiver's code, you set the activity that is going to open when clicking the notification by setting a pending intent. Check this note of deprecation .
The current implementation, using the new ParsePushBroadcastReceiver , is more flexible than ever! So instead of extending the BroadcastReceiver, extend this one. Be sure to read the documentation about this. It is very helpful.

Also:
a) In your manifest replace the portion of the "My custom receiver" with the following

<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) Change this

<receiver android:name="com.parse.ParseBroadcastReceiver" >

to your own custom receiver

<receiver android:name="com.makemyandroidapp.parsenotificationexample.ParseReceiver">



c) In your manifest make sure to use the ParseApplication class you have created as the name attribute:

<application
    android:name=".ParseApplication"
    ....
    ....
 />



I hope the above will help you continue with your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM