Last active
March 24, 2021 04:27
-
-
Save erikfloresq/c631adb2c3405d66a22f561c9a31c7c5 to your computer and use it in GitHub Desktop.
Android notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirebaseMessagingService extends FirebaseMessagingService { | |
@Override | |
public void onMessageReceived(@NotNull RemoteMessage remoteMessage) { | |
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ | |
Log.d(ContentValues.TAG, "From: " + remoteMessage.getFrom()); | |
// Check if message contains a data payload. | |
if (!remoteMessage.getData().isEmpty()) { | |
Log.d(ContentValues.TAG, "Message data payload: " + remoteMessage.getData()); | |
} | |
// Check if message contains a notification payload. | |
if (remoteMessage.getNotification() != null) { | |
Log.d(ContentValues.TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); | |
} | |
} | |
@Override | |
public void onNewToken(@NotNull String token) { | |
Log.d(ContentValues.TAG, "Refreshed token: $token"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirebaseMessagingService : FirebaseMessagingService() { | |
override fun onMessageReceived(remoteMessage: RemoteMessage) { | |
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ | |
Log.d(ContentValues.TAG, "From: " + remoteMessage.from!!) | |
// Check if message contains a data payload. | |
if (remoteMessage.data.isNotEmpty()) { | |
Log.d(ContentValues.TAG, "Message data payload: " + remoteMessage.data) | |
} | |
// Check if message contains a notification payload. | |
if (remoteMessage.notification != null) { | |
Log.d(ContentValues.TAG, "Message Notification Body: " + remoteMessage.notification!!.body!!) | |
} | |
} | |
override fun onNewToken(token: String) { | |
Log.d(ContentValues.TAG, "Refreshed token: $token") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main2); | |
// Subscribe | |
FirebaseMessaging.getInstance().subscribeToTopic("VisionNews") | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
String msg = getString(R.string.msg_subscribed); | |
if (!task.isSuccessful()) { | |
msg = getString(R.string.msg_subscribe_failed); | |
} | |
Log.d(ContentValues.TAG, msg); | |
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
// Get firebase token | |
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() { | |
@Override | |
public void onComplete(@NonNull Task<InstanceIdResult> task) { | |
if (!task.isSuccessful()) { | |
Log.w(ContentValues.TAG, "getInstanceId failed", task.getException()); | |
} | |
// Get new Instance ID token | |
String token = task.getResult().getToken(); | |
// Log and toast | |
String msg = getString(R.string.msg_token_fmt, token); | |
Log.d(ContentValues.TAG, msg); | |
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Subscribe | |
FirebaseMessaging.getInstance().subscribeToTopic("VisionNews") | |
.addOnCompleteListener { task -> | |
var msg = getString(R.string.msg_subscribed) | |
if (!task.isSuccessful) { | |
msg = getString(R.string.msg_subscribe_failed) | |
} | |
Log.d(ContentValues.TAG, msg) | |
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() | |
} | |
// Get firebase token | |
FirebaseInstanceId.getInstance().instanceId | |
.addOnCompleteListener{ task -> | |
if (!task.isSuccessful) { | |
Log.w(ContentValues.TAG, "getInstanceId failed", task.exception) | |
} | |
// Get new Instance ID token | |
val token = task.result?.token | |
// Log and toast | |
val msg = getString(R.string.msg_token_fmt, token) | |
Log.d(ContentValues.TAG, msg) | |
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment