微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 解析推送通知异常:“未授权:需要主密钥”

我想使用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!");
        }
    });
    }
 }

解决方法

更新的答案

嗨,我调查一下,发现目前唯一发送Push的是使用masterKey,这正是你得到这个错误的原因

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

>在您的云代码main.js文件中创建一个功能

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

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

>通过以下方式从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 
    }
});

这将触发您在上面和云代码内部创建的云代码函数调用,useMasterKey为true,因此它应该可以工作.

更新:拼写

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐