我跟着这个link关于如何使用GCM发送推送通知的tut,我设法发送和接收来自应用程序的通知,但是,我无法接收来自不同Android设备的通知,仅设备发送和接收自己。我需要它使用相同的项目ID发送到所有设备? 这是我用过的代码
unit Unit1;
interface
uses
System.SysUtils, System.Classes, GCMNotification, IndyPeerImpl,
IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL
;
type
TDataModule1 = class(TDataModule)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
{$IFDEF ANDROID}
gcmn: TGCMNotification;
{$ENDIF}
public
{ Public declarations }
function GetDeviceRegistrationId: string;
function PushMessage(Pushmessage: string): string;
end;
var
DataModule1: TDataModule1;
const
YOUR_GCM_SENDERID = '1232010xxxxxx';
YOUR_API_ID = 'AIzaSyBzvpTa-xxxxxxxxxxxxxxxxx';
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
isRegistered : boolean;
begin
isRegistered := true;
{$IFDEF ANDROID}
try
gcmn := TGCMNotification.Create(self);
gcmn.SenderID := YOUR_GCM_SENDERID;
isRegistered := gcmn.DoRegister;
except
end;
{$ENDIF}
end;
procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
{$IFDEF ANDROID}
gcmn.Free();
{$ENDIF}
end;
function TDataModule1.GetDeviceRegistrationId: string;
begin
{$IFDEF ANDROID}
result := gcmn.RegistrationID;
{$ELSE}
result := 'Mobile Test';
{$ENDIF}
end;
function TDataModule1.PushMessage(Pushmessage : string):string;
const
sendUrl = 'https://android.googleapis.com/gcm/send';
var
Params: TStringList;
AuthHeader: STring;
idHTTP: TIDHTTP;
SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
idHTTP := TIDHTTP.Create(nil);
try
SslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
idHTTP.IOHandler := SSLIOHandler;
idHTTP.HTTPOptions := [];
Params := TStringList.Create;
try
Params.Add('registration_id='+ GetDeviceRegistrationId());
Params.Values['data.message'] := Pushmessage;
idHTTP.Request.Host := sendUrl;
AuthHeader := 'Authorization: key=' + YOUR_API_ID;
idHTTP.Request.CustomHeaders.Add(AuthHeader);
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded;charset=UTF-8';
result := idHTTP.Post(sendUrl, Params);
finally
Params.Free;
end;
finally
FreeAndNil(idHTTP);
end;
end;
end.
这是按钮中的代码
DataModule1.PushMessage(Edit1.Text);
答案 0 :(得分:0)
我不熟悉Delphi,但看起来你需要修改PushMessage
方法。
在Params.Add(...)中将registration_id
更改为registration_ids
,并确保GetDeviceRegistrationId()
返回预期注册ID收件人的数组。