如何使用推送通知打开活动

时间:2016-06-15 15:56:08

标签: android push-notification

我正在处理一个应用推送通知的应用示例。

一切正常,我执行python脚本并收到推送通知。

当我推入通知时出现问题。我不能让通知打开应用程序......

项目结构是:

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();
    }

} }

TokenRefreshListenerService.java

package com.example.sergi.pruebapush;

import android.content.Intent;

import com.google.android.gms.iid.InstanceIDListenerService;


 public class TokenRefreshListenerService extends InstanceIDListenerService    
 {
  @Override
public void onTokenRefresh() {
    Intent i = new Intent(this, RegistrationService.class);
    startService(i);
}
}

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()

我可以添加哪些内容来打开推送通知的应用程序?

1 个答案:

答案 0 :(得分:-1)

您发送的通知无法用于启动应用,您必须使用数据有效负载,然后使用onMessageRecieved和sendNotification。

GcmListenerService Example from google

示例包括onMessageReceived,在从服务器收到推送通知时调用,该调用将调用sendNotification。 sendNotification实现,待定意图。这将启动MainActivity。

编辑:

Notification payload support

或者您可以添加点击进入通知

send.py

data={
"to" : "/topics/my_little_topic",
"notification" : {
    "body" : messageBody,
    "title" : messageTitle,
    "icon" : "ic_launcher"
    "click_action" : "com.example.test"
}
}

为您要启动的活动添加意图过滤器

的manifest.xml

<intent-filter>
  <action android:name="com.example.test" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>