I am working on a Hello World application that receives PUSH notifications.
Everything works correctly, and where I install it, I receive notifications when I run the python script.
The problem that when the notification appears I do not know how to make you open the app.
The structure of the project is as follows:
- MainActivity.java
- NotificationsListenerService.java
- RegistrationService.java
- TokenRefreshListenerService.java
and a python send.py script
MainActivity.java:
package com.example.sergi.pruebapush;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, RegistrationService.class);
startService(i);
}
}
NotificationsListenerService.java
import com.google.android.gms.gcm.GcmListenerService;
public class NotificationsListenerService extends GcmListenerService {
}
RegistrationService.java
package com.example.sergi.pruebapush;
import android.app.IntentService; import android.content.Intent; import android.util.Log;
import com.google.android.gms.gcm.GcmPubSub; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID;
import java.io.IOException;
public class RegistrationService extends IntentService {
public RegistrationService() {
super("RegistrationService");
}
@Override
protected void onHandleIntent(Intent intent) {
InstanceID myID = InstanceID.getInstance(this);
String registrationToken="";
try {
registrationToken = myID.getToken(
getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE,
null
);
Log.d("Registration Token", registrationToken);
GcmPubSub subscription = GcmPubSub.getInstance(this);
subscription.subscribe(registrationToken, "/topics/my_little_topic", null);
} catch (IOException e) {
e.printStackTrace();
}
} }
Finally the script that I run every time I want a notification sent:
send.py
from urllib2 import *
import urllib
import json
import sys
MY_API_KEY="************************************************"
messageTitle = sys.argv[1]
messageBody = sys.argv[2]
data={
"to" : "/topics/my_little_topic",
"notification" : {
"body" : messageBody,
"title" : messageTitle,
"icon" : "ic_launcher"
}
}
dataAsJSON = json.dumps(data)
request = Request(
"https://gcm-http.googleapis.com/gcm/send",
dataAsJSON,
{ "Authorization" : "key="+MY_API_KEY,
"Content-type" : "application/json"
}
)
print urlopen(request).read()
Where can I control the notification to open the app?