构建应用程序以提供出色的体验非常重要,而与新用户和现有用户进行互动对于应用程序的长期成功更为重要。 在本教程中,您将学习如何使用Firebase启用Google Cloud Messaging并将通知发送到您的应用程序,以及如何允许用户与使用Android或iOS设备的联系人共享您的应用程序。
Firebase通知
通知是让您的用户了解应用程序中的新事件或功能,并让他们再次打开应用程序的最简单方法之一。 使用Firebase,您可以向所有用户或部分用户群发送通知,从而可以自定义用户收到的信息并激起他们的兴趣。
应用程序在后台时的通知
当您的应用程序在后台运行时,显示通知非常容易,因为它是由Firebase Messaging库自动处理的。 在您的应用程序中,您将需要在库中的dependencies
包含以下行 您的build.gradle文件的节点,然后将应用程序安装在设备上。
compile 'com.google.firebase:firebase-messaging:9.8.0'
接下来,您需要进入Firebase控制台,并在左侧导航面板中选择“ 通知”部分。
进入正确的区域后,您应该会看到一个屏幕,提示您发送第一条消息 。
单击蓝色按钮后,将带您进入一个表单,该表单可让您向邮件中添加内容并选择应接收该邮件的组。 使用此表格,您可以指定设备或用户必须满足的布尔条件(例如地理位置或任何其他收集的数据)才能接收通知。
发送消息后,通知应出现在用户的设备上。
前台通知
需要注意的一件事是,如果用户已经在您的应用程序中,则不会显示通知。 为了在这种情况下接收通知,您将需要实现一个扩展FirebaseMessagingService
的Service
。
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
您还需要在AndroidManifest.xml文件中包含此Service
。
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
现在,您已经拥有了通用框架,现在该FirebaseMessagingService
类中的onMessageReceived
了。 此方法的主要目的是获取通过RemoteMessage
对象向下发送的数据,并根据收到的内容创建一个Notification
。
RemoteMessage
可以传递很多信息。 但是,大多数选项仅在使用Firebase后端API而非控制台的情况下可用。 从Firebase控制台,您可以设置标题,消息正文和自定义键/值对。
可以从RemoteMessage
记录所有可用数据,如下所示:
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
Log.e("Test", "Key = " + entry.getKey() + ", Value = " + entry.getValue() );
}
Log.e("TutsPlus", "collapsekey: " + remoteMessage.getCollapseKey());
Log.e("TutsPlus", "from: " + remoteMessage.getFrom() );
Log.e("TutsPlus", "message id: " + remoteMessage.getMessageId() );
Log.e("TutsPlus", "message type:: " + remoteMessage.getMessageType() );
Log.e("TutsPlus", "to: " + remoteMessage.getTo() );
Log.e("TutsPlus", "send time: " + remoteMessage.getSentTime() );
Log.e("TutsPlus", "ttl: " + remoteMessage.getTtl() );
Log.e("TutsPlus", "title: " + remoteMessage.getNotification().getTitle() );
Log.e("TutsPlus", "body: " + remoteMessage.getNotification().getBody() );
Log.e("TutsPlus", "click action: " + remoteMessage.getNotification().getClickAction() );
Log.e("TutsPlus", "color: " + remoteMessage.getNotification().getColor() );
Log.e("TutsPlus", "icon: " + remoteMessage.getNotification().getIcon() );
依次将产生以下日志消息。
E/TutsPlus: Key = custom_data_key, Value = some_value
E/TutsPlus: collapsekey: com.tutsplus.tutsplusfirebasenotification
E/TutsPlus: from: 1044252370931
E/TutsPlus: message id: 0:1478812920837872%ba5f8433ba5f8433
E/TutsPlus: message type:: null
E/TutsPlus: to: null
E/TutsPlus: send time: 1478812919847
E/TutsPlus: ttl: 0
E/TutsPlus: title: Custom title
E/TutsPlus: body: Message Text
E/TutsPlus: click action: null
E/TutsPlus: color: null
E/TutsPlus: icon: null
一旦知道了可用的信息以及如何处理它,就可以为用户创建并显示通知。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(remoteMessage.getNotification().getTitle());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText(remoteMessage.getNotification().getBody());
NotificationManagerCompat.from(this).notify(0, builder.build());
就是这样! 您现在应该可以设置一个Android应用程序,以从Firebase控制台发送和接收通知。
应用邀请
在您的应用上吸引新用户可能是一个艰巨的挑战,但是口口相传是使事情顺利进行的好方法。 使用Firebase,您可以将“应用程序邀请”小部件添加到您的应用程序中,这将使您的用户可以通过电子邮件或短信与其他Android或iOS用户共享您的应用程序。
启动邀请提示
在开始使用此功能之前,您需要通过将以下行添加到项目中,将包导入到项目中。 dependencies
build.gradle文件的节点。
compile 'com.google.firebase:firebase-invites:9.8.0'
同步项目后,您将能够使用AppInviteInvitation.IntentBuilder
类创建新的Intent
,该类将启动一个屏幕,允许用户选择邀请来加入该应用的联系人。 该构建器提供了各种选项,用于定制应用程序邀请屏幕:
-
setMessage
:这将设置用户看到的消息,并可以通过短信或电子邮件发送给联系人。 长度不能超过100个字符。 -
setCustomImage
:使用此方法,您可以为将显示在邀请屏幕和邀请电子邮件中的自定义图像提供URI。 -
setCallToActionText
:此方法设置电子邮件中安装按钮的文本。 最多32个字符。 -
setDeepLink
:允许您为邀请提供元数据,安装时可以接收元数据,以便为新邀请的用户执行特定操作。 -
setEmailHtmlContent
:允许您覆盖setMessage
,setCustomImage
和setCallToActionText
来创建自定义HTML格式的电子邮件,以发送给潜在的新用户。 -
setEmailSubject
:如果使用setEmailHtmlContent
则为必需。 顾名思义,这将为您的自定义电子邮件设置主题。 -
setOtherPlatformsTargetApplication
:一种更有趣的选项,该方法将允许您将Firebase客户端应用程序ID与您的iOS版本的应用程序相关联,如果iOS用户由Android用户共享,则允许iOS用户安装正确的版本。
创建Intent
,您可以使用startActivityForResult
启动它,以便在用户邀请其他人返回时得到通知。
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
.setCallToActionText(getString(R.string.invitation_cta))
.setDeepLink(Uri.parse("/link"))
.setOtherPlatformsTargetApplication(
AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS,
getString(R.string.ios_app_client_id))
.build();
接收邀请
现在您可以邀请其他用户使用您的应用程序,让我们花点时间专注于深层链接选项。 创建Intent
,您可以添加URI
作为深层链接。 当用户在Android上收到您的邀请时,您可以使用Google Play服务和AppInvite API拦截此URI
并为新用户执行自定义操作,例如显示欢迎屏幕。
boolean autodeeplink = true;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(AppInvite.API)
.enableAutoManage(this, this)
.build();
AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autodeeplink)
.setResultCallback(
new ResultCallback<AppInviteInvitationResult>() {
@Override
public void onResult(AppInviteInvitationResult result) {
if (result.getStatus().isSuccess()) {
//Get intent information
Intent intent = result.getInvitationIntent();
String deepLink = AppInviteReferral.getDeepLink(intent);
String invitationId = AppInviteReferral.getInvitationId(intent);
}
}
}
);
您会注意到我们创建了一个名为autodeeplink
的boolean
。 设置为true
,Android系统将通过AndroidManifest.xml文件中的过滤器自动处理接收到的URI
。 如果将其设置为false
,则可以使用AppInvite API从邀请中提取信息并执行自定义操作。
结论
在本教程中,您学习了如何将Firebase中的通知实施到您的Android应用程序中,以及如何使您的用户轻松地与他们的联系人共享您的应用程序。 了解Firebase中的可用功能以及如何实现常规功能将极大地帮助您快速构建人们喜欢使用的应用程序。
翻译自: https://code.tutsplus.com/tutorials/firebase-for-android-notifications-and-app-invites--cms-27593