Merhaba. Googlei Google Cloud Messaging (GCM) yeni versiyon adıyla Firebase Cloud Messaging (FCM) androidde kullanmak istiyorum. Bazı dökümanlar buldum işe yaramadı. Aslında hata veriyor diyemiyorum. Çünkü uygulama açılıyor siyah ekranda kalıyor. Bu durum PaaS (Parse) servisini kullanıncada oluşuyor. O yüzden işin içinden çıkamıyorum.
Forma bir tane memo ve button bileşeni ekledim. Options-Entitlement List den Receive push notifications true yaptım.
Örnek kodum bu. Kaynak olarak bu site
Program açınca siyah ekranda kalıyor malesef.
Forma bir tane memo ve button bileşeni ekledim. Options-Entitlement List den Receive push notifications true yaptım.
Örnek kodum bu. Kaynak olarak bu site
unit uMain;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo,
System.PushNotification, System.Notification
{$IFDEF ANDROID}
,FMX.PushNotification.Android
{$ENDIF};
type
TMain = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure OnServiceConnectionChange(Sender: TObject; PushChanges: TPushService.TChanges);
procedure OnReceiveNotificationEvent(Sender: TObject; const ServiceNotification: TPushServiceNotification);
procedure ShowAndroidNotification(MessageText: string; NotificationNumber: Integer);
public
{ Public declarations }
end;
var
Main: TMain;
const
app_id = 'GCM ID';
implementation
{$R *.fmx}
{ TMain }
procedure TMain.OnServiceConnectionChange(Sender: TObject; PushChanges: TPushService.TChanges);
begin
//Do something if the service connection changes
end;
procedure TMain.OnReceiveNotificationEvent(Sender: TObject; const ServiceNotification: TPushServiceNotification);
var
MessageText: string;
begin
Memo1.Lines.Add('DataKey = ' + ServiceNotification.DataKey);
Memo1.Lines.Add('Json = ' + ServiceNotification.Json.ToString);
Memo1.Lines.Add('DataObject = ' + ServiceNotification.DataObject.ToString);
{$IFDEF ANDROID}
MessageText := ServiceNotification.DataObject.GetValue('gcm.notification.body').Value;
{$ENDIF};
Memo1.Lines.Add(DateTimeToStr(Now) + ' Message = ' + MessageText);
ShowAndroidNotification(MessageText, 0);
end;
procedure TMain.ShowAndroidNotification(MessageText: string; NotificationNumber: Integer);
var
NotificationCenter: TNotificationCenter;
Notification: TNotification;
begin
NotificationCenter := TNotificationCenter.Create(nil);
try
Notification := NotificationCenter.CreateNotification;
try
Notification.Name := MessageText;
Notification.AlertBody := MessageText;
Notification.Title := MessageText;
Notification.EnableSound := false;
Notification.Number := NotificationNumber;
NotificationCenter.ApplicationIconBadgeNumber := NotificationNumber;
NotificationCenter.PresentNotification(Notification);
finally
Notification.DisposeOf;
end;
finally
NotificationCenter.Free;
NotificationCenter.DisposeOf;
end;
end;
procedure TMain.Button1Click(Sender: TObject);
var
PushService: TPushService;
ServiceConnection: TPushServiceConnection;
DeviceId, DeviceToken: string;
begin
{$IFDEF ANDROID}
PushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
PushService.AppProps[TPushService.TAppPropNames.GCMAppID] := app_id;
{$ENDIF}
ServiceConnection := TPushServiceConnection.Create(PushService);
ServiceConnection.Active := True;
ServiceConnection.OnChange := OnServiceConnectionChange;
ServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
DeviceId := PushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceId];
DeviceToken := PushService.DeviceTokenValue[TPushService.TDeviceTokenNames.DeviceToken];
Memo1.Lines.Add('DeviceID: ' + DeviceId);
Memo1.Lines.Add('FCM Token: ' + DeviceToken);
Memo1.Lines.Add('Ready to receive!');
end;
end.
Program açınca siyah ekranda kalıyor malesef.

