How to receive parse push notifiactions on an android device using parse.com

12,390

Solution 1

Use like this :

  public class Application extends android.app.Application {

  public Application() {
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");

    PushService.setDefaultPushCallback(this, MainActivity.class);
  }
}

//MainActivity.java - Activity you need to open when the notification is clicked.

In your Manifest file add this application.

<application    android:name="com.parse.tutorials.pushnotifications.Application"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.parse.tutorials.pushnotifications.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> </application>

And add this line in your MainActivity class which you need to open when the notification is clicked.

Bundle mBundle = getIntent().getExtras();
if (mBundle != null) {
    String mData = mBundle.getString("com.parse.Data");
    System.out.println("DATA : xxxxx : " + mData);
}

Solution 2

You have to write a parse broadcast receiver in order to receive notification.. Write in your manifest

  <receiver
        android:name="com.example.package.ParseBroadcastReceiver"
        android:exported="false"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.example.package.MESSAGE" />
        </intent-filter>
    </receiver>

then define a broadcast receiver

public class ParseBroadcastReceiver extends BroadcastReceiver{

public static final String ACTION                       =   "com.example.package.MESSAGE";
public static final String PARSE_EXTRA_DATA_KEY         =   "com.parse.Data";
public static final String PARSE_JSON_CHANNEL_KEY       =   "com.parse.Channel";

@Override
public void onReceive(Context context, Intent intent) {

     String action = intent.getAction();
        String channel = intent.getExtras().getString(PARSE_JSON_CHANNEL_KEY);
        JSONObject json = new JSONObject(intent.getExtras().getString(PARSE_EXTRA_DATA_KEY));

    }

Now this json object will contain the data that you have send from parse.. For instance if you have to send notification using javascript api

   Parse.Push.send({where: query, // Set our Installation query
                data: {                   
                    triggerKey:triggerValue, 
                    objectType:"android",
                    action:"com.example.package.MESSAGE"
                }
              },{
              success: function() {
                // Push was successful

              },
              error: function(error) {
                // Handle error

              }
            });

Note that in your push notification you have to mention the "action" key and it should be same as the one you have mentioned in your broadcast receiver intent filter.

Share:
12,390
Ashwin S Ashok
Author by

Ashwin S Ashok

Android Developer.

Updated on June 12, 2022

Comments

  • Ashwin S Ashok
    Ashwin S Ashok almost 2 years

    How to intent from parse push notification. i f anybody implemented parse push please help.

    Parse.initialize(Splash.this,"id","id");
    ParseInstallation.getCurrentInstallation().saveInBackground();
    PushService.setDefaultPushCallback(Splash.this, ParsePush.class);
    

    implementing like this.

    can't get any values in jsonData.

    public class ParsePush extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            ParseInstallation.getCurrentInstallation().saveInBackground();
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            String jsonData = extras.getString("com.parse.Data");
            System.out.println("Data Json : " + jsonData);
        }
    
    }
    

    need to implement an intent from the push notification (parse). that is need to show an activity on clicking the push.. please help.

  • Ashwin S Ashok
    Ashwin S Ashok about 10 years
    still not working. need to place an intent for push notification.
  • test
    test about 10 years
    how you are sending push notification
  • Ashwin S Ashok
    Ashwin S Ashok about 10 years
    I'm sending push from parse.com.
  • test
    test about 10 years
    can you show me json data you are pushing. You need to add action:"com.example.package.MESSAGE" field in your json data
  • Zen
    Zen almost 10 years
    Any ref on how to mention the 'action' if im sending the Push from a device? (Android)
  • Qadir Hussain
    Qadir Hussain almost 10 years
    how to get the text sent via push notification from parse.com please explain futher