Pergunta

This is how my setup looks.

The LunchActivity has code:

Parse.initialize(this, "MY_APP_ID", "MY_APP_KEY");
PushService.subscribe(this, "MyCity", HomeActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();

HomeActivity class is a simple activity class that opens a simple screen used as default. I have also written a custom receiver.

public class CityPushReceiver extends BroadcastReceiver {
    private static final String TAG = "CityPushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            JSONObject json = new JSONObject(intent.getExtras().getString(
                    "com.parse.Data"));

            Integer event_id = Integer.parseInt((String) json.get("event_id"));

            Intent eventIntent = new Intent(context, EventResult.class);
            eventIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            eventIntent.putExtra("event_id", event_id);
            context.getApplicationContext().startActivity(eventIntent);

        } catch (JSONException e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }
    }
}

Manifest file has entry:

<receiver
    android:name="com.myapp.CityPushReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="com.myapp.CITY_NOTIFICATION" />
    </intent-filter>
</receiver>

I use Python code to push the notification:

import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps({
       "channels": [
         "MyCity"
       ],
       "data": {
     "action": "com.myapp.CITY_NOTIFICATION",
         "alert": "New Event Notification",
     "event_id": "425"
       }
     }), {
       "X-Parse-Application-Id": "APP_ID",
       "X-Parse-REST-API-Key": "API_KEY",
       "Content-Type": "application/json"
     })
result = json.loads(connection.getresponse().read())
print result

This setup is not working as expected. I do get the notification on my device (I am using AVD for testing). But it opens the expected EventResult activity even without me clicking on the notification in the tray. This happens even if I am on device home screen and the app is running only in background. And when I click on the notification in the tray it opens the HomeActivity class which is defined as default class.

Expected behaviour is opening the EventResult only when I click on the notification in the tray. Can you guys tell me what needs to change?

Foi útil?

Solução 3

So frankly all the answers posted here are right in one or the other scenario so I though I will put things in perspective here.

About my specific question, the right way to look at it is if you implement your own broadcast receiver it will be called whenever you receive the push and whatever code is inside it, it will be executed. To make sure that you see a notification there are two important things:

  1. Make sure you are using alerts when you send the push
  2. Either use default receiver given by Parse. i.e. in configuration just tell parse which activity to call, send the push with alert and let parse handle rest of the stuff. It will show the alert and it will open the particular activity when you click on the alert. The other way is more complicated and that is to write your own receiver with all the considerations to the alert.

A lot depends on how you send the notification too. So make sure the alerts, the data, sometimes the activity is properly mentioned.

Outras dicas

I found that taking control over the notification creation was best on Android. If you don't send a title or alert field in the push then the parse.com android SDK won't create a notification and you can create one yourself when the push comes in.

This was the method I was using to create a notification:

void CreateNotication(Context context, String title, String pushId) {
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MyClass.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle(title)
            .setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(pushId, 1, mBuilder.build());
}

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:label="@string/app_name"
    android:name="com.parse.tutorials.pushnotifications.Application"
    android:theme="@style/AppTheme">

    <activity
        android:label="@string/app_name"
        android:name="com.parse.tutorials.pushnotifications.MainActivity">

        <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 Activity class which you need to open when the notification is clicked.

ParseAnalytics.trackAppOpened(getIntent()); 

Check this Answer : LINK

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top