【问题标题】:Not receiving notification未收到通知
【发布时间】:2014-03-03 12:25:16
【问题描述】:

我们正在使用解析来存储聊天消息,并且我们正在使用解析通知。

对于 iOS,我们这样做是为了在 parse 的安装表中创建条目。它在 parse 的安装表中创建条目,我认为必须接收通知。

 PFInstallation *currentInstallation = [PFInstallation currentInstallation];

 currentInstallation.deviceToken = user.notificationToken;

[currentInstallation saveInBackground];

但在 android 中,我无法在安装表中创建条目。

我没有收到来自 parse 的通知,我认为这是没有收到通知的原因..

是这样吗?

谁能引导我走向正确的道路?

更新

现在收到通知,但仍有一些疑问。 我在用户登录时执行此操作。如果未创建用户,则我已设置条件,否则仅创建不创建。我没有将它添加到此,因为这不是必需的

   // setting up parse installation
private void setUpParseInstallation()
{

    ParseInstallation currentInstallation = ParseInstallation
            .getCurrentInstallation();

    currentInstallation.saveInBackground(new SaveCallback()
    {

        @Override
        public void done(com.parse.ParseException e)
        {
            if (e != null)
            {
                Log.e(TAG, "Error saving parse installation");
            }
        }
    });
}




// add user to parse it it doesn't exist otherwise handle the cases
private void addUserToParseIfNotExisting()
{

            // if user doesn't exist create a new user
        ParseObject newuser = new ParseObject("user");
            newuser.put("name", email);
            newuser.put("userId", id);

            newuser.saveInBackground();
}


private void setChannel()
{
     channel = "abdf";
         RS_User user = new RS_User(this);

    ParseQuery<ParseObject> pquery = ParseQuery.getQuery("user");

    pquery.whereEqualTo("userId", user.getUserId());

    // see if user exists in user table on parse
    pquery.findInBackground(new FindCallback<ParseObject>()
    {

        @Override
        public void done(List<ParseObject> list,
                com.parse.ParseException error)
        {

            if (list.size() > 0)
            {
                        list.get(i).put("channels", channel);
                        list.get(i).saveInBackground();
                                            PushService.subscribe(getApplicationContext(),channel, RS_HelpActivity.class);

            }
       } 
   });
}

发送通知

ParsePush push = new ParsePush();
                                if(object.get(k).has("channels"))
                                {
                                    push.setChannel(object.get(k).getString(
                                            "channels"));
                                }
                                push.setData(dataAndroid);
                                push.sendInBackground();

目前我正在做这一切来使用 Parse 发送/接收通知。有没有办法不使用通道直接使用 notification token 或其他使用 parse 的方法?因为我将拥有用户通知令牌。

【问题讨论】:

    标签: android notifications push-notification parse-platform android-notifications


    【解决方案1】:

    希望这段代码对你有帮助

    Application.java

    public class Application extends android.app.Application {
    
      public Application() {
      }
    
      @Override
      public void onCreate() {
        super.onCreate();
    
        // Initialize the Parse SDK. 
        Parse.initialize(this, "your applicationId", "your clientKey"); 
    
        // Specify an Activity to handle all pushes by default.
        PushService.setDefaultPushCallback(this, MainActivity.class);
      }
    }
    

    AndroidManifest.xml

    <application
        android:name="<Your Package Name>.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    

    【讨论】:

    • 如果我有使用 GCM 生成的该用户的通知令牌,我可以向该用户发送通知吗?
    【解决方案2】:

    您是否尝试使用advanced targeting 向任何安装发送通知,其设备令牌与包含在 (in) 查询(或您需要的任何其他条件)相匹配。

    类似这样的:

     ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
     query.whereEqualTo("your_key", "your_value");
     // OR any of whereXXX method
    
     ParsePush push = new ParsePush();
     push.setData(dataAndroid);
     push.setQuery(query);
     push.sendInBackground();
    

    编辑

    ParseInstallation 因为其他 ParseObject(s) 有 put 方法。

    由于在 Android 中您没有像 iOS 这样的 UDID,因此您必须弄清楚如何创建类似的东西。很遗憾,我无法向您发送我们如何生成“我们的”设备令牌,但 here 您可以找到有效的建议。

      ParseInstallation install = ParseInstallation.getCurrentInstallation();
      install.put("device-token", yourGeneratedDeviceId);
      install.saveInBackground(new SaveCallback() { ... });
    

    【讨论】:

    • 我必须在安装中手动存储设备令牌还是通过解析自己管理?
    • 我们在 ParseInstallation 注册期间手动存储我们的设备令牌。
    • 我们正在 iOS 中这样做,但我不知道如何在 android 中做到这一点。你能给它点灯吗?
    • @BSThakrar 查看编辑
    • 我知道如何生成设备令牌。它只是在解析安装中设置它的问题。谢谢我会试试这个。
    猜你喜欢
    • 2021-10-30
    • 2014-05-21
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多