Prithvi
Prithvi

Reputation: 101

want to send User to User notification using parse for android devices

I am trying to send user to user notification on click of a button to specific users but getting this issue while sending notification from last 4 days in android, and with this, whenever i uninstall the app from my phone which i am using as emulator, and reinstall it for debugging, it doesnt create new installation id and take the old one: i Guess this is the problem, please it will be great help if anybody help me in a elaborated way

enter image description here

Here is my code of custom reciever:

public class CustomPushReceiver extends ParsePushBroadcastReceiver {
    private final String TAG = CustomPushReceiver.class.getSimpleName();

    private NotificationUtils notificationUtils;

    private Intent parseIntent;

    public CustomPushReceiver() {
        super();
    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);

        if (intent == null)
            return;

        try {
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            Log.e(TAG, "Push received: " + json);

            parseIntent = intent;

            parsePushJson(context, json);

        } catch (JSONException e) {
            Log.e(TAG, "Push message json exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPushDismiss(Context context, Intent intent) {
        super.onPushDismiss(context, intent);
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        super.onPushOpen(context, intent);
    }

    /**
     * Parses the push notification json
     *
     * @param context
     * @param json
     */
    private void parsePushJson(Context context, JSONObject json) {
        try {
            boolean isBackground = json.getBoolean("is_background");
            JSONObject data = json.getJSONObject("data");
            String title = data.getString("title");
            String message = data.getString("message");

            if (!isBackground) {
                Intent resultIntent = new Intent(context, MainActivity.class);
                showNotificationMessage(context, title, message, resultIntent);
            }

        } catch (JSONException e) {
            Log.e(TAG, "Push message json exception: " + e.getMessage());
        }
    }


    /**
     * Shows the notification message in the notification bar
     * If the app is in background, launches the app
     *
     * @param context
     * @param title
     * @param message
     * @param intent
     */
    private void showNotificationMessage(Context context, String title, String message, Intent intent) {

        notificationUtils = new NotificationUtils(context);

        intent.putExtras(parseIntent.getExtras());

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        notificationUtils.showNotificationMessage(title, message, intent);
    }
}

and my ParseQuery:

try {
                    ParseQuery userQuery = new ParseQuery(ParseUser.class);
                    userQuery.whereNear("Location",
                            currentUser.getParseGeoPoint("Location"));
                    userQuery.setLimit(10);
                    userQuery.whereEqualTo("Type", "Doctor");
                    userQuery.whereEqualTo("deviceType", "android");
                    userQuery.findInBackground();
                    // Find devices associated with these users
                    ParseQuery pushQuery = ParseInstallation.getQuery();
                    pushQuery.whereMatchesQuery("user", userQuery);
                    // Send push notification to query
                    ParsePush push = new ParsePush();
                    push.setQuery(pushQuery);
                    push.setMessage("Only users near"
                            + currentUser.getParseGeoPoint("Location")
                            + " will recieve this push notification");
                    push.sendInBackground(new SendCallback() {

                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                Toast.makeText(getApplicationContext(),
                                        "Notification Sent", Toast.LENGTH_LONG)
                                        .show();
                            } else {
                                Toast.makeText(getApplicationContext(),
                                        "Notification not Sent",
                                        Toast.LENGTH_LONG).show();
                            }
                        }

                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }

my Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aspire.wecare"
    android:versionCode="1"
    android:versionName="1.0" >

    <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.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.aspire.wecare.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.aspire.wecare.permission.C2D_MESSAGE" />

    <permission
        android:name="com.aspire.wecare.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.aspire.wecare.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:name="com.aspire.wecare.ParseApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.aspire.wecare.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>



        <activity
            android:name="com.aspire.wecare.Welcome"
            android:label="@string/title_activity_welcome" >
        </activity>
        <activity
            android:name="com.aspire.wecare.LoginActivity"
            android:label="@string/title_login" >
        </activity>
        <activity
            android:name="com.aspire.wecare.LoginSignupActivity"
            android:label="@string/title_activity_login_signup" >
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBA3_YfEl2cnZ8uDmrI23g8W7l1y2Xiku8" />
      <!-- Added for Parse push notifications -->

        <service android:name="com.parse.PushService" />

        <receiver
            android:name=".receiver.CustomPushReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.aspire.wecare" />
            </intent-filter>
        </receiver>

        <!-- /Added for Parse push notifications -->
    </application>

</manifest>

Upvotes: 1

Views: 390

Answers (1)

Fattie
Fattie

Reputation: 12582

In general terms,

(1) you would be doing this in cloud code. Fortunately it's very easy once you get started. In fact here is a total explanation from a similar question.

https://stackoverflow.com/a/32540054/294884

Muhc more importantly though,

(2) be aware you're using the wrong tool for the job. You do not use 'Push' for what you are trying to achieve. You should be using PubNub, Firebase, Pusher or a similar service. It is commonplace to use one of these in combination with Parse.

Upvotes: 2

Related Questions