- Android
- Flutter
この機能はNotifly Android SDK 1.5.0以降で利用できます。
package com.your.project
import android.app.Application
import android.util.Log
import tech.notifly.Notifly
import tech.notifly.push.interfaces.INotificationClickEvent
import tech.notifly.push.interfaces.INotificationClickListener
class SampleApplication : Application() {
override fun onCreate() {
super.onCreate()
// 任意でログレベルを設定します
Notifly.setLogLevel(Log.VERBOSE)
// Notifly SDKを初期化します
Notifly.initialize(
context = applicationContext,
projectId = BuildConfig.NOTIFLY_PROJECT_ID,
username = BuildConfig.NOTIFLY_USERNAME,
// passwordは後方互換性のためのプレースホルダーです。実際のパスワードやAPI Secret Keyを設定しないでください。
password = BuildConfig.NOTIFLY_USERNAME,
)
// プッシュ通知のクリックイベントを購読します
Notifly.addNotificationClickListener(object : INotificationClickListener {
override fun onClick(event: INotificationClickEvent) {
Log.d("SampleApplication", "Notification clicked!")
Log.d("SampleApplication", "Notification title: ${event.notification.title}")
Log.d("SampleApplication", "Notification body: ${event.notification.body}")
Log.d(
"SampleApplication",
"Notification custom data: ${event.notification.customData}"
)
}
})
}
}
この機能はNotifly Flutter SDK 1.4.0以降で利用できます。
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:go_router/go_router.dart';
import 'package:notifly_flutter/notifly_flutter.dart';
import 'package:notifly_flutter_example/src/DetailPage.dart';
import 'package:notifly_flutter_example/src/HomePage.dart';
void main() async {
await dotenv.load();
runApp(const MyApp());
}
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/:id',
builder: (context, state) {
final pathId = state.pathParameters['id']!;
return DetailPage(id: pathId);
},
)
],
);
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<void> initNotifly() async {
WidgetsFlutterBinding.ensureInitialized();
await NotiflyPlugin.initialize(
projectId: dotenv.env['NOTIFLY_PROJECT_ID']!,
username: dotenv.env['NOTIFLY_USERNAME']!,
// `password`は後方互換性のためのプレースホルダーです。実際のパスワードやAPI Secret Keyを設定しないでください。
password: dotenv.env['NOTIFLY_USERNAME']!,
);
print('🔥 [Flutter] NotiflyPlugin initialized');
if (!kIsWeb && Platform.isAndroid) {
await NotiflyPlugin.addNotificationClickListener((event) {
final customData = event.notification.customData;
if (customData != null) {
final routeId = customData['routeId'];
if (routeId != null) {
router.go('/$routeId');
}
}
});
}
}
@override
void dispose() {
super.dispose();
}
@override
void initState() {
super.initState();
initNotifly();
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
プッシュ通知のクリックイベントのインターフェース
- Android
- Flutter
- React Native
INotificationClickEventのnotificationオブジェクトから、ユーザーがタップした通知の情報を取得できます。IPushNotificationの各プロパティを説明します。表示 IPushNotificationのプロパティ
表示 IPushNotificationのプロパティ
Importanceはtech.notifly.push.impl.Importanceで定義されています。値はImportance.HIGH、Importance.NORMAL、Importance.LOWのいずれかです。重要度が設定されていない場合はnullになります(1.5.0以降)。OSNotificationClickEventのnotificationメンバーから、ユーザーがタップした通知の情報を取得できます。次の型定義はプロパティを示したものです。コンストラクターと実装は省略しています。/// 通知のクリックを処理するコールバックです。
typedef NotificationClickListener = void Function(
OSNotificationClickEvent event,
);
/// 通知の重要度を表します。Androidでのみ利用できます。
enum OSNotificationImportance {
/// 通知の重要度は低です。
low,
/// 通知の重要度は通常です。
normal,
/// 通知の重要度は高です。
high,
}
/// Notiflyサービスから受信した通知を表します。
class OSNotification extends JSONStringRepresentable {
/// 通知のNotiflyメッセージIDです。
String? notiflyMessageId;
/// Androidシステムの通知IDです。Androidでのみ利用できます。
int? androidNotificationId;
/// 通知を送信したNotiflyキャンペーンのIDです。
String? campaignId;
/// 通知のタイトルです
String? title;
/// 通知の本文です
String? body;
/// 通知の重要度です。Androidでのみ利用できます。
OSNotificationImportance? importance;
/// 通知に含まれるURLです。
String? url;
/// 通知に含まれる画像のURLです。
String? imageUrl;
/// 通知の送信時刻です。Unixエポックからのミリ秒数で表します
int? sentTime;
/// 通知のTTLです。秒単位で表します
int? ttl;
/// 通知に含まれるカスタムデータです。
Map<String, dynamic>? customData;
/// 通知の生のペイロードです。
Map<String, dynamic>? rawPayload;
}
/// 通知のクリックイベントを表します。
class OSNotificationClickEvent {
OSNotification notification;
}
OSNotificationの各プロパティを説明します。表示 OSNotificationのプロパティ
表示 OSNotificationのプロパティ
OSNotificationImportance.high、OSNotificationImportance.normal、OSNotificationImportance.lowのいずれかです。Androidでのみ利用できます(1.4.0以降)。OSNotificationClickEventのnotificationメンバーから、ユーザーがタップした通知の情報を取得できます。export type NotificationClickListener = (
event: OSNotificationClickEvent,
) => void;
export declare enum OSNotificationImportance {
LOW = 0,
NORMAL = 1,
HIGH = 2,
}
export interface OSNotificationClickEvent {
notification: OSNotification;
}
export interface OSNotification {
notiflyMessageId?: string;
androidNotificationId?: number;
campaignId?: string;
title?: string;
body?: string;
importance?: OSNotificationImportance;
url?: string;
imageUrl?: string;
sentTime?: number;
ttl?: number;
customData?: {
[key: string]: any;
};
rawPayload?: {
[key: string]: any;
};
}
OSNotificationの各プロパティを説明します。表示 OSNotificationのプロパティ
表示 OSNotificationのプロパティ
FCMメッセージの
dataフィールドに含まれるカスタムデータのキーと値のペアです。Notiflyコンソールで設定できます(3.2.0以降)。OSNotificationImportance.HIGH、OSNotificationImportance.NORMAL、OSNotificationImportance.LOWのいずれかです。Androidでのみ利用できます(3.2.0以降)。FCMメッセージの
dataフィールドの生データです。パースされたオブジェクトとして返されます。パースに失敗した場合は、元のペイロードが返されます(3.2.0以降)。