Tencent Push Notification (QQ) service ended in November 2020
repositories {
google()
mavenCentral()
}
dependencies {
implementation 'com.nhncloud.android:nhncloud-push-fcm:1.9.4'
...
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation 'com.nhncloud.android:nhncloud-push-adm:1.9.4'
...
}
buildscript {
// ...
dependencies {
// ...
classpath "com.google.gms:google-services:$google_services_version" // google-services plugin
}
}
allprojects {
// ...
repositories {
// ...
google() // Google's Maven repository
}
}
apply plugin: 'com.android.application'
android {
// ...
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
dependencies {
//...
compileOnly files('amazon/libs/amazon-device-messaging-1.2.0.jar')
}
-libraryjars amazon/libs/amazon-device-messaging-1.2.0.jar
-dontwarn com.amazon.device.messaging.**
-keep class com.amazon.device.messaging.** { *; }
-keep public class * extends com.amazon.device.messaging.ADMMessageReceiver
-keep public class * extends com.amazon.device.messaging.ADMMessageHandlerBase
-keep public class * extends com.amazon.device.messaging.ADMMessageHandlerJobBase
NhnCloudPushConfiguration configuration =
NhnCloudPushConfiguration.newBuilder(context, "YOUR_APP_KEY")
.build();
NhnCloudPush.initialize(PushType.FCM, configuration);
NhnCloudPushConfiguration configuration =
NhnCloudPushConfiguration.newBuilder(context, "YOUR_APP_KEY")
.build();
NhnCloudPush.initialize(PushType.ADM, configuration);
NhnCloudPush.initialize(NhnCloudPushConfiguration) has been deprecated. PushType is automatically set to FCM when initialized using NhnCloudPush.initialize(NhnCloudPushConfiguration).
public void onLogin(String userId) {
// Login.
NhnCloudSdk.setUserId(userId);
// Token registration, etc.
}
// Create a receiving agreement setting object
NhnCloudPushAgreement agreement = NhnCloudPushAgreement.newBuilder(true) // Agree to receive notification messages
.setAllowAdvertisements(true) // Agree to receive advertising notification messages
.setAllowNightAdvertisements(true) // Agree to receive nigh-time advertising notification messages
.build();
// Register a token and set receiving agreement
NhnCloudPush.registerToken(context, agreement, new RegisterTokenCallback() {
@Override
public void onRegister(@NonNull PushResult result,
@Nullable String token) {
if (result.isSuccess()) {
// Token registration succeeded
} else {
// Token registration failed
int code = result.getCode();
String message = result.getMessage();
}
}
});
NhnCloudPush.queryTokenInfo(context, new QueryTokenInfoCallback() {
@Override
public void onQuery(@NonNull PushResult result,
@Nullable TokenInfo tokenInfo) {
if (result.isSuccess()) {
// Token information query succeeded
String token = tokenInfo.getToken();
NhnCloudPushAgreement agreement = tokenInfo.getAgreement();
} else {
// Token information query failed
int code = result.getCode();
String message = result.getMessage();
}
}
});
If you do not want to receive messages after the service logout, you must unregister the token.
Even if the token is unregistered, the notification permission on the device is not revoked.
Unregistering a token that has already been unregistered returns success with the message "Already a token has been unregistered".
NhnCloudPush.unregisterToken(mContext, new UnregisterTokenCallback() {
@Override
public void onUnregister(@NonNull PushResult result,
@Nullable String unregisteredToken) {
if (result.isSuccess()) {
// Token unregistration succeeded
} else {
// Token unregistration failed
int code = result.getCode();
String message = result.getMessage();
}
}
});
Application#onCreate
.When receiving a message, a notification is not exposed if the user is using the app (Foreground). Foreground status can be checked by isForeground passed to OnReceiveMessageListener#onReceive.
public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
NhnCloudPush.setOnReceiveMessageListener(new OnReceiveMessageListener() {
@Override
public void onReceive(@NonNull NhnCloudPushMessage message,
boolean isForeground) {
// Expose a notification even when the user is using the app
if (isForeground) {
NhnCloudNotification.notify(getApplicationContext(), message);
}
}
});
// ...
}
}
if (Build.VERSION.SDK_INT >= 33) {
NhnCloudNotification.requestPostNotificationsPermission(this, PERMISSION_REQUEST_CODE);
}
if (Build.VERSION.SDK_INT <= 32) {
NotificationChannel channel = NhnCloudNotification.getNotificationChannel(this);
if (channel == null) {
NhnCloudNotification.createNotificationChannel(this);
}
}
Application#onCreate
.public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
NhnCloudNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(@NonNull NhnCloudPushMessage message) {
// Service logic such as page move can be performed based on the message content.
Map<String, String> extras = message.getExtras();
}
});
// ...
}
}
Application#onCreate
or defined as metadata in the AndroidManifest.xml file.If you do not set the default notification channel name, it is automatically set as the name of the application.
public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
NhnCloudNotification.setDefaultChannelName(context, "YOUR_CHANNEL_NAME");
// ...
}
}
<!-- Set the default channel name -->
<meta-data android:name="com.toast.sdk.push.notification.default_channel_name"
android:value="@string/default_notification_channel_name"/>
Application#onCreate
or defined as metadata in the AndroidManifest.xml file.If you change all notification options
public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
NhnCloudNotificationOptions defaultOptions = new NhnCloudNotificationOptions.Builder()
.setPriority(NotificationCompat.PRIORITY_HIGH) // Set notification priority
.setColor(0x0085AA) // Set notification background color
.setLights(Color.RED, 0, 300) // Set LED light
.setSmallIcon(R.drawable.ic_notification) // Set small icon
.setSound(context, R.raw.dingdong1) // Set notification sound
.setVibratePattern(new long[] {500, 700, 1000}) // Set vibration pattern
.enableForeground(true) // Set foreground notification exposure
.enableBadge(true) // Set badge icon use
.build();
NhnCloudNotification.setDefaultOptions(context, defaultOptions);
// ...
}
}
If you change only some of the configured notification options
public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
// Get the configured default notification options
NhnCloudNotificationOptions defaultOptions = NhnCloudNotification.getDefaultOptions(context);
// Create a builder from the notification options object
NhnCloudNotificationOptions newDefaultOptions = defaultOptions.buildUpon()
.enableForeground(true) // Change only the foreground notification exposure status
.build();
NhnCloudNotification.setDefaultOptions(context, newDefaultOptions);
// ...
}
}
<!-- Notification priority -->
<meta-data android:name="com.toast.sdk.push.notification.default_priority"
android:value="1"/>
<!-- Notification background color -->
<meta-data android:name="com.toast.sdk.push.notification.default_background_color"
android:resource="@color/defaultNotificationColor"/>
<!-- LED light -->
<meta-data android:name="com.toast.sdk.push.notification.default_light_color"
android:value="#0000ff"/>
<meta-data android:name="com.toast.sdk.push.notification.default_light_on_ms"
android:value="0"/>
<meta-data android:name="com.toast.sdk.push.notification.default_light_off_ms"
android:value="500"/>
<!-- Small icon -->
<meta-data android:name="com.toast.sdk.push.notification.default_small_icon"
android:resource="@drawable/ic_notification"/>
<!-- Notification sound -->
<meta-data android:name="com.toast.sdk.push.notification.default_sound"
android:value="notification_sound"/>
<!-- Vibration pattern -->
<meta-data android:name="com.toast.sdk.push.notification.default_vibrate_pattern"
android:resource="@array/default_vibrate_pattern"/>
<!-- Badge icon use -->
<meta-data android:name="com.toast.sdk.push.notification.badge_enabled"
android:value="true"/>
<!-- Notification exposure while the app is running -->
<meta-data android:name="com.toast.sdk.push.notification.foreground_enabled"
android:value="false"/>
Type | Feature | Action |
---|---|---|
Open App (OPEN_APP) | Run the application | PushAction.ActionType.OPEN_APP |
Open URL (OPEN_URL) | Go to URL (run Web URL address or app's custom scheme) |
PushAction.ActionType.OPEN_URL |
Reply (REPLY) | Send a reply from notification | PushAction.ActionType.REPLY |
Cancel (DISMISS) | Cancel current notification | PushAction.ActionType.DISMISS |
Up to 3 buttons per message are supported.
It takes time to download media files when using a web URL.
If the image used is not 1:1 ratio, the image may be displayed differently from what you expect because it is forcibly changed to 1:1.
Application#onCreate
.public class MyApplication extends Application {
@Override
public void onCreate() {
// ...
NhnCloudNotification.setOnActionListener(new OnActionListener() {
@Override
public void onAction(@NonNull PushAction action) {
// In case of Reply action, send the content to a service server
if (action.getActionType() == PushAction.ActionType.REPLY) {
// Get the reply content that the user entered
String userText = action.getUserText();
// Send the user input content to a service server
}
}
});
// ...
}
}
(Caution) 1. If the onMessageReceived function does not request (notify) the notification after receiving the message, the notification is not exposed. 2. If you create a notification manually, you must set the Push service intent as the notification's content intent in order to collect metrics. (See the Adding the Metric Collection Feature section below)
public class MyPushMessageReceiver extends NhnCloudPushMessageReceiver {
@Override
public void onMessageReceived(@NonNull Context context,
@NonNull NhnCloudRemoteMessage remoteMessage) {
// Change channel ID
remoteMessage.setChannelId("channel");
// Modify message content
NhnCloudPushMessage message = remoteMessage.getMessage();
CharSequence title = message.getTitle();
message.setTitle("[Modified] " + title);
// Set the execution intent (If not set, package's default main activity is executed)
Intent launchIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
context,
REQUEST_CODE,
launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// If you want to expose the notification based on whether the user is using the app or not
if (!isAppForeground()) {
// Create and expose the notification
notify(context, remoteMessage, contentIntent);
} else {
// Expose a specific UI screen
NhnCloud.makeText(context, message.title, NhnCloud.LENGTH_SHORT).show();
}
}
}
(Caution) 1. When using NhnCloudPushMessageReceiver, you must set permission. 2. When targeting API level 31 or higher, you must set the 'exported' attribute.
<manifest>
<application>
<receiver android:name=".NhnCloudPushSampleReceiver"
android:permission="${applicationId}.toast.push.permission.RECEIVE"
android:exported="false">
<intent-filter>
<action android:name="com.toast.android.push.MESSAGE_EVENT" />
</intent-filter>
</receiver>
<!-- Omitted -->
</application>
<!-- Omitted -->
</manifest>
public class MyPushMessageReceiver extends NhnCloudPushMessageReceiver {
private NotificationManager mManager = null;
@Override
public void onMessageReceived(
@NonNull Context context,
@NonNull NhnCloudRemoteMessage remoteMessage) {
// Obtain the message content
NhnCloudPushMessage message = remoteMessage.getMessage();
// Create NotificationManager
if (mManager == null) {
mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mManager == null) {
Log.e(TAG, "Failed to get NotificationManager");
return;
}
}
// Set the channel
String channelId = "YOUR_CHANNEL_ID";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mManager.getNotificationChannel(channelId);
if (channel == null) {
String channelName = "YOUR_CHANNEL_NAME";
createNotificationChannel(channelId, channelName);
}
}
// Set the launch intent
Intent launchIntent = new Intent(context, MainActivity.class);
// Create a content intent that includes sending metrics
PendingIntent contentIntent;
contentIntent = getContentIntent(context, remoteMessage, launchIntent);
// Create a notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
builder.setContentTitle(message.getTitle())
.setContentText(message.getBody())
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(contentIntent)
.setAutoCancel(true);
notify(context, NotificationUtils.createNewId(), builder.build());
}
...
}
(Caution) If an emoji that is not supported by the device is used, it may not be displayed.
// Create a tag ID list to add
Set<String> tagIds = new HashSet<>();
tagIds.add(TAG_ID_1); // e.g. "ZZPP00b6" (8-character string)
tagIds.add(TAG_ID_2);
// Add the tag ID list of the logged-in user ID
NhnCloudPush.addUserTag(tagIds, new UserTagCallback() {
@Override
public void onResult(@NonNull PushResult result, @Nullable Set<String> tagIds) {
if (result.isSuccess()) {
// Adding the user tag ID succeeded
} else {
// Adding the user tag ID failed
int errorCode = result.getCode();
String errorMessage = result.getMessage();
}
}
});
// Update the tag ID list of the logged-in user ID (Existing tag ID list is deleted and set to the inputted value)
NhnCloudPush.setUserTag(tagIds, new UserTagCallback() {
@Override
public void onResult(@NonNull PushResult result, @Nullable Set<String> tagIds) {
if (result.isSuccess()) {
// Updating the user tag ID list succeeded
} else {
// Updating the user tag ID list failed
int errorCode = result.getCode();
String errorMessage = result.getMessage();
}
}
});
// Return the whole tag ID list of the logged-in user ID
NhnCloudPush.getUserTag(new UserTagCallback() {
@Override
public void onResult(@NonNull PushResult result, @Nullable Set<String> tagIds) {
if (result.isSuccess()) {
// Retrieving the user tag ID list succeeded
} else {
// Retrieving the user tag ID list failed
int errorCode = result.getCode();
String errorMessage = result.getMessage();
}
}
});
// Create a tag ID list to delete
Set<String> tagIds = new HashSet<>();
tagIds.add(TAG_ID_1); // e.g. "ZZPP00b6" (8-character string)
tagIds.add(TAG_ID_2);
// Delete the tag ID list of the logged-in user ID
NhnCloudPush.removeUserTag(tagIds, new UserTagCallback() {
@Override
public void onResult(@NonNull PushResult result, @Nullable Set<String> tagIds) {
if (result.isSuccess()) {
// Deleting the user tag ID list succeeded
} else {
// Deleting the user tag ID list failed
int errorCode = result.getCode();
String errorMessage = result.getMessage();
}
}
});
// Delete the whole tag ID list of the logged-in user ID
NhnCloudPush.removeAllUserTag(new UserTagCallback() {
@Override
public void onResult(@NonNull PushResult result, @Nullable Set<String> tagIds) {
if (result.isSuccess()) {
// Deleting the whole user tag succeeded
} else {
// Deleting the whole user tag failed
int errorCode = result.getCode();
String errorMessage = result.getMessage();
}
}
});
/* NhnCloudPushConfiguration.java */
public String getAppKey();
public static Builder newBuilder(@NonNull Context context, @NonNull String appKey);
Method | Returns | |
---|---|---|
getAppKey | String | Returns the Push service Appkey. |
static newBuilder | NhnCloudPushConfiguration.Builder | Creates a builder to create a NhnCloudPushConfiguration object. |
/* PushResult.java */
public int getCode();
public String getMessage();
public boolean isSuccess();
public boolean isFailure();
Method | Returns | |
---|---|---|
getCode | int | Returns the result code. |
getMessage | int | Returns the result message. |
isSuccess | boolean | Returns whether or not it succeeded. |
isFailure | boolean | Returns whether or not it failed. |
/* TokenInfo.java */
public String getPushType();
public NhnCloudPushAgreement getAgreement();
public String getTimeZone();
public String getCountry();
public String getLanguage();
public String getUserId();
public Date getActivatedDateTime();
public String getToken();
Method | Returns | |
---|---|---|
getPushType | String | Returns a Push type. |
getAgreement | NhnCloudPushAgreement | Returns whether to agree to notifications/advertising/night-time advertising, etc. |
getTimeZone | String | Returns the timezone. |
getCountry | String | Returns the country code. |
getLanguage | String | Returns the language code. |
getUserId | String | Returns the user ID. |
getActivatedDateTime | Date | Returns the date and time of the token's most recent registration. |
getToken | String | Returns a token. |
/* NhnCloudRemoteMessage.java */
public String getChannelId();
public void setChannelId(String channelId);
public NhnCloudPushMessage getMessage();
public String getSenderId();
Method | Returns | |
---|---|---|
getChannelId | String | Returns the channel ID. |
setChannelId | Sets the channel ID. | |
getMessage | NhnCloudPushMessage | Returns a message object. |
getSenderId | String | Returns the sender ID. (FCM Only) |
/* NhnCloudPushMessage.java */
public String getMessageId();
public String getPusyType();
public String getTitle();
public void setTitle(String title);
public String getBody();
public void setBody(String body);
public RichMessage getRichMessage();
public Map<String, String> getExtras();
Method | Returns | |
---|---|---|
getMessageId | String | Returns the message identifier. |
getPusyType | String | Returns the PushType. |
getTitle | String | Returns the message title. |
setTitle | Sets the message title. | |
getBody | String | Returns the message content. |
setBody | Sets the message content. | |
getRichMessage | RichMessage | Returns rich message information. |
getExtras | Returns all received messages. |
/* PushAction.java */
public ActionType getActionType();
public String getNotificationId();
public String getNotificationChannel();
public NhnCloudPushMessage getMessage();
public String getUserText();
Method | Returns | |
---|---|---|
getActionType | ActionType | Returns the ActionType. |
getNotificationId | String | Returns the ID of the notification where the action was executed. |
getNotificationChannel | String | Returns the channel of the notification where the action was executed. |
getMessage | NhnCloudPushMessage | Returns the message information of the notification where the action was executed. |
getUserText | RichMessage | Returns the string entered by the user. |
/* NhnCloudPushMessageReceiver.java */
public final boolean isAppForeground();
public final void notify(Context context, NhnCloudRemoteMessage message);
public final void notify(Context context, NhnCloudRemoteMessage message, PendingIntent contentIntent);
public final void notify(Context context, int notificationId, Notification notification);
@Deprecated
public final PendingIntent getNotificationServiceIntent(Context context, NhnCloudRemoteMessage message, PendingIntent contentIntent);
public final PendingIntent getContentIntent(Context context, NhnCloudRemoteMessage message, Intent launchIntent);
Method | Returns | Parameters | |
---|---|---|---|
isAppForeground | boolean | Returns whether the app is currently in use. | |
notify | Context, NhnCloudRemoteMessage | Creates and exposes notifications with the default execution intents. | |
notify | Context, NhnCloudRemoteMessage, PendingIntent | Creates and exposes notifications with a user execution intent. | |
notify | Context, int, Notification | Exposes user notifications with a specific ID. | |
@Deprecated getNotificationServiceIntent |
PendingIntent | Context, NhnCloudRemoteMessage, PendingIntent | Returns a user launch intent that includes sending metrics. It does not work normally from Android 12 (API level 31) or higher, so you must use getContentIntent() instead. |
getContentIntent | PendingIntent | Context, NhnCloudRemoteMessage, Intent | Returns a user launch intent that includes sending metrics. |
/* NhnCloudNotificationOptions.java */
public int getPriority();
public int getSmallIcon();
public int getColor();
public int getLightColor();
public int getLightOnMs();
public int getLightOffMs();
public long[] getVibratePattern();
public Uri getSound();
public boolean isForegroundEnabled();
public boolean isBadgeEnabled();
public Builder buildUpon();
Method | Returns | Parameters | |
---|---|---|---|
getPriority | int | Returns the priority. | |
getSmallIcon | int | Returns the resource identifier for the small icon. | |
getColor | int | Returns the background color. | |
getLightColor | int | Returns the LED color. | |
getLightOnMs | int | Returns the time when the LED light is turned on. | |
getLightOffMs | int | Returns the time when the LED light is turned off. | |
getVibratePattern | long[] | Returns the pattern of vibrations. | |
getSound | Uri | Returns the URI of the notification sound. | |
isForegroundEnabled | boolean | Returns whether or not to use foreground notifications. | |
isBadgeEnabled | boolean | Returns whether or not to use the badge icon. | |
buildUpon | NhnCloudNotificationOptions#Builder | Returns a builder based on the current option information. |