如何在没有应用服务器的情况下使用GCM将消息发送到通知密钥

时间:2016-06-06 17:21:58

标签: android google-cloud-messaging

我跟着Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401管理Android上的本地设备组并成功获得了通知密钥,但是当我向通知密钥发送消息时,我从未收到消息。 有没有人有这项工作?

我的发送代码如下:

use feature qw(say);
use strict;
use warnings;

my %seqs = (
    "PrefixPE/1"  => "CAGCMGCCGCGGTAAYWC",
    "PrefixPE/2" => "BSCCCGYCAATTYMTKTRAGT"
);

my %ops = (
    R => 'AG',
    Y => 'CT',
    M => 'AC',
    K => 'GT',
    W => 'AT',
    B => 'CGT',
    S => 'CG',
);

$ops{$_} = [ split //, $ops{$_} ] for keys %ops;

my $perm = GenPermutations->new( \%ops  );
for my $id (keys %seqs) {
    my $seq = $seqs{$id};
    $perm->gen( $seq );
    $perm->print_result();
}
exit;

package GenPermutations;

sub new {
    my ( $class, $ops ) = @_;
    my ($pat) = map qr/$_/, '[' . (join '', keys %$ops) . ']';
    my $info = { ops => $ops, pat => $pat, pos => [], result => [], seq => undef }; 
    return bless $info, $class;
}

sub _init {
    my ( $self, $seq ) = @_;
    @{ $self->{pos} } = ();
    @{ $self->{result} } = ();
    $self->{seq} = $seq;
    while ( $seq =~ /($self->{pat})/g ) {
        push @{ $self->{pos} }, [$-[1], $1];
    }
}

sub print_result {
    my ( $self ) = @_;

    say $self->{seq} . ' : found ' . (scalar @{ $self->{result} }) . ' permutations : ';
    say "  $_" for @{ $self->{result} };
    say "";
}

sub gen {
    my ( $self, $seq ) = @_;
    $self->_init( $seq );
    $self->_gen( $seq, 0 );
}

sub _gen {
    my ( $self, $str, $pos_index ) = @_;

    if ( $pos_index > $#{$self->{pos}} ) {
        push @{ $self->{result} }, $str;
        return;
    }

    my $info = $self->{pos}[$pos_index];
    my ( $index, $letter) = @$info;
    $pos_index++;
    for my $replace ( @{ $self->{ops}{$letter} } ) {
        my $temp = $str;
        substr $temp, $index, 1, $replace; 
        $self->_gen( $temp, $pos_index );
    }
}

1;

2 个答案:

答案 0 :(得分:0)

似乎对GCM概念存在误解。应用服务器是integral part of GCM messaging

  

Google Cloud Messaging(GCM)的服务器端包含两个   组件:

     
      
  • Google提供的GCM连接服务器。这些服务器接收消息   从应用服务器将其发送到在设备上运行的客户端应用。   Google为HTTP和XMPP提供连接服务器。
  •   
  • 申请表   您必须在您的环境中实施的服务器。这个应用程序   服务器通过所选的GCM连接将数据发送到客户端应用程序   服务器,使用适当的XMPP或HTTP协议。
  •   

尝试Android GCM Playground以便更好地了解这一点。

这是一个片段:

public void sendMessage() {
        String senderId = getString(R.string.gcm_defaultSenderId);
        if (!("".equals(senderId))) {
            String text = upstreamMessageField.getText().toString();
            if (text == "") {
                showToast("Please enter a message to send");
                return;
            }

            // Create the bundle for sending the message.
            Bundle message = new Bundle();
            message.putString(RegistrationConstants.ACTION, RegistrationConstants.UPSTREAM_MESSAGE);
            message.putString(RegistrationConstants.EXTRA_KEY_MESSAGE, text);

            try {
                gcm.send(GcmPlaygroundUtil.getServerUrl(senderId),
                        String.valueOf(System.currentTimeMillis()), message);
                showToast("Message sent successfully");
            } catch (IOException e) {
                Log.e(TAG, "Message failed", e);
                showToast("Upstream FAILED");
            }
        }
    }

答案 1 :(得分:0)

send方法的to字段表示项目的发件人ID。您无法使用此方法向Instance ID令牌(其他设备)发送消息,GCM目前不支持设备到设备消息传递。

您可以避免在客户端应用中包含API密钥,因此目前您需要一台应用服务器来发送这些类型的消息。