2016-08-03 103 views
2

我想使用Parse API和GCM發送推送通知。我已經通過發送推送通知形式解析終端並在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(); 
      } 
     } 
    }); 

機器人的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> 

初始化解析SDK中的Android

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!"); 
     } 
    }); 
    } 
} 
+0

https://groups.google.com/forum/#!topic/back4app/AbWfQSRjsAU –

+1

嗨@IntelliJ阿米亞·我已經設置主鍵存在,但面臨着同樣的異常 –

+1

@IntelliJ阿米亞·有你任何有關在android代碼中使用Master_Key的想法。意味着我們需要在Android應用程序中設置Master_key,如Application_id和client_key –

回答

6

修訂ANSWER

嗨,我進行調查了一下,發現目前唯一發送推送是通過使用masterKey而這正是爲什麼您收到此錯誤

爲了與主密鑰最好發送推送的原因方法是創建一個雲代碼函數並從客戶端觸發該函數。 所以你需要做到以下幾點:

  1. 裏面你的雲代碼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); 
 
    }); 
 
});

後所創建的雲碼功能重新啓動您的服務器

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

這將觸發上面創建的雲碼功能和雲代碼裏面useMasterKey通話是true所以它應該工作。

更新:拼寫

+0

我已經配置此代碼 –

+0

萬能鑰匙怎麼樣?你還指定了主密鑰嗎? –

+0

是的,我已經在index.js配置了masterkey –