解析推送通知例外:“未授权:需要主密钥”

时间:2016-08-03 08:14:48

标签: android parse-platform push

我想使用Parse API和GCM发送推送通知。我已经在服务器上成功配置并通过Parse终端发送推送通知并在Android设备上接收测试。

但是当我以编程方式发送推送时会出现例外情况:“未经授权:需要主密钥”

我使用以下代码:

ParseQuery query = ParseInstallation.getQuery();
    query.whereEqualTo("channels", "testing");

    // Notification for Android users
    query.whereEqualTo("deviceType", "android");
    ParsePush androidPush = new ParsePush();
    androidPush.setMessage("Your suitcase has been filled with tiny robots!");
    androidPush.setQuery(query);
    androidPush.sendInBackground(new SendCallback() {
        @Override
        public void done(ParseException e) {
            if ( e == null ) {
                Log.d("GARG","Parse Notification Done. : ");
            } else {
                Log.d("GARG","Notification failed.: "+e.getMessage());
                e.printStackTrace();
            }
        }
    });

android manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo">

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

<uses-permission android:name="com.demo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

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

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

<application
    android:name=".AppInitializer"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true">

<service android:name="com.parse.PushService" />
    <meta-data
        android:name="com.parse.push.notification_icon"
        android:resource="@drawable/ic_cast_light" />

    <meta-data android:name="com.parse.push.gcm_sender_id"
        android:value="id:211671060483" />

    <meta-data
        android:name="com.parse.APPLICATION_ID"
        android:value="@string/parse_app_id" />
    <meta-data
        android:name="com.parse.CLIENT_KEY"
        android:value="@string/parse_client_key" />

    <meta-data
        android:name="com.parse.X-Parse-Master-Key"
        android:value="@string/parse_master_key" />

<receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <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.demo" />
        </intent-filter>
    </receiver>

</application>

在android中初始化Parse SDK

public class AppInitializer extends Application {

public static JSONObject errorDataProvider;

@Override
public void onCreate() {

    super.onCreate();
    Parse.enableLocalDatastore(getApplicationContext());
    Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
            .applicationId(Utility.ApplicationId)
            .clientKey(Utility.ClientKey)
            .server(Utility.Server_URL)
            .build()
    );

    ParseInstallation.getCurrentInstallation().saveInBackground();

    ParsePush.subscribeInBackground(Utility.PARSE_CHANNEL, new SaveCallback() {
        @Override
        public void done(ParseException e) {
            Log.e("GARG", "Successfully subscribed to Parse!");
        }
    });
    }
 }

2 个答案:

答案 0 :(得分:7)

更新的答案

嗨,我稍微调查一下,发现目前发送Push的唯一方法就是使用 masterKey ,这就是你收到此错误的原因

为了使用主密钥发送推送,最好的方法是创建云代码功能并从客户端触发此功能。 所以你需要做以下事情:

  1. 在您的云代码main.js文件中创建一个新功能
  2. Parse.Cloud.afterSave("SendPush", function(request) {
    
    
      var query = new Parse.Query(Parse.Installation);
      query.exists("deviceToken");
    
      // here you can add other conditions e.g. to send a push to sepcific users or channel etc.
    
      var payload = {
        alert: "YOUR_MESSAGE"
          // you can add other stuff here...
      };
    
    
      Parse.Push.send({
          data: payload,
          where: query
        }, {
          useMasterKey: true
        })
        .then(function() {
          response.success("Push Sent!");
        }, function(error) {
          response.error("Error while trying to send push " + error.message);
        });
    });

    创建云代码功能后重启服务器

    1. 以下列方式从您的Android应用程序触发此云代码功能:

      HashMap<String,String> map = new HashMap<String, String>();
      map.put("PARAM1KEY","PARAM1VALUE");
      // here you can send parameters to your cloud code functions
      // such parameters can be the channel name, array of users to send a push to and more...
      
      ParseCloud.callFunctionInBackground("SendPush",map, new FunctionCallback<Object>() {
      
          @Override
          public void done(Object object, ParseException e) {
              // handle callback 
          }
      });
      
    2. 这将触发您在上面和云代码内部创建的云代码函数的调用 useMasterKey true ,因此它应该可以正常工作。

      更新:拼写

答案 1 :(得分:0)

  

useMasterKey:是

这将使解析服务器能够使用主键从客户端推送通知。