无法使用parse.com将推送从用户发送到用户

时间:2016-01-10 12:33:50

标签: android parse-platform push-notification

这里我真的不明白为什么我的代码不是从用户发送推送到另一个用户,我可以通过发送渠道发送和接收推送但问题是我得到推送通知给所有用户,而不是特定用户,如何我可以从用户发送到下面的另一个用户是我的代码请解释我的错误是什么,或者我的工作方式

MY onClick of button必须发生的事情

ok.setOnClickListener(
            new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {

                    final String currentUserId = ParseUser.getCurrentUser().getObjectId();
                    ParseQuery<ParseUser> query = ParseUser.getQuery();
                    getPhone = phone.getText().toString();

                    //for not including myself
                    query.whereNotEqualTo("objectId", currentUserId);
                    query.whereEqualTo("username", getPhone);
                    query.getFirstInBackground(new GetCallback<ParseUser>()
                    {
                        public void done(final ParseUser user, ParseException e)
                        {
                            if (user == null)
                            {
                                Toast.makeText(Welcome.this, "couldnot connect to " + getPhone, Toast.LENGTH_SHORT).show();
                                Log.d("username", "problem retriving username");
                            }
                            else
                            {
                                ParseQuery pushQuery = ParseInstallation.getQuery();
                                pushQuery.whereEqualTo("email", "three");
                                 final String name = user.getUsername();

                                String data = "{\n" +
                                        "\"data\":{\n "+
                                                "\"message\":\"Connection request\",\n" +
                                                "\"title\":\"Connection\",\n" +
                                                "\"from\":"+"\""+ParseUser.getCurrentUser().getUsername()+"\""+"\n "+
                                                "}\n" +
                                          "}";
                                JSONObject jsonObject = null ;
                                try
                                {
                                    jsonObject = new JSONObject(data);
                                } catch (JSONException e1)
                                {
                                    e1.printStackTrace();
                                }


                                ParsePush push = new ParsePush();
                                push.setQuery(pushQuery);
                                push.setData(jsonObject);
                                //push.setChannel("Giants");
                                push.sendInBackground(new SendCallback()
                                                      {
                                                          @Override
                                                          public void done(ParseException e)
                                                          {

                                                              if (e == null)
                                                              {
                                                                  Toast.makeText(getApplicationContext(), "request send to "+name, Toast.LENGTH_SHORT).show();
                                                              }
                                                              else
                                                              {
                                                                  Toast.makeText(getApplicationContext(), "problem sending request to "+name+" due to "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                                              }

                                                          }
                                                      }
                                );
                                Intent i = new Intent(Welcome.this, TestActivity.class);
                                Log.e("about user", "connected to " + user.getUsername());
                                retrivedUser = user.getUsername();
                                i.putExtra("number", retrivedUser);
                                startActivity(i);
                            }
                        }
                    });

我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

<!--
  IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
  to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
    android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.chattapp.permission.C2D_MESSAGE" />


<application
    android:name=".Chattapp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" />
    <activity android:name=".DispatchActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LoginActivity" />
    <activity android:name=".SignUpActivity" />
    <activity android:name=".Welcome" />
    <activity android:name=".TestActivity"></activity>

    <service android:name="com.parse.PushService" />
    <!--<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 android:name="com.example.chattapp.CustomReceiver"
        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" />


              <!--IMPORTANT: Change "com.parse.starter" to match your app's package name.-->

            <category android:name="com.example.chattapp" />
        </intent-filter>
    </receiver>

</application>

这是推送图片

push details

2 个答案:

答案 0 :(得分:1)

这就是我为自己制作的聊天应用所做的,每当用户在您的应用上唱歌时,都会将其录制到具有用户ID的频道。这样,您将为每个用户提供一个独特的渠道,您可以向他们发送单独的推送。

这是我提出的快速代码:

        ParseUser user = new ParseUser();
        user.setUsername("my name");
        user.setPassword("my pass");
        user.setEmail("email@example.com");

        // other fields can be set just like with ParseObject
        user.put("phone", "650-253-0000");

        user.signUpInBackground(new SignUpCallback() {
          public void done(ParseException e) {
            if (e == null) {
                String userID = user.getObjectId();
                ParsePush.subscribeInBackground(userID});
            } else {
              // Sign up didn't succeed. Look at the ParseException
              // to figure out what went wrong
            }
          }
        });

在此之后你只需要接收器的用户ID就可以了。

答案 1 :(得分:0)

是否在解析应用程序设置中启用了客户端推送? 它默认是禁用的。 更多信息可以在解析博客中找到:

http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/