- Aug 1, 2019
- 1
- 0
- 0
I want to create a SDK in android studio and useit in my other mobile applications.
I'm some what new to android studio. In the SDK I want to manage notifications, such as getting notification from server, create channels and show notification. When my code is in the application, it works, but when I moved it to the android library, it doesn't work and in running mode application closed. what should I do? What steps do I need to correctly create SDK for my applications?
code to create channels
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
code to show notifications
btnShow.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View view) {
try {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification")
.setContentText("test")
.setChannelId(CHANNEL_ID)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
catch (Exception ex)
{
Log.i("Log Error",ex.getMessage());
}
}
});
These codes work on application itself but not in the library.
I'm some what new to android studio. In the SDK I want to manage notifications, such as getting notification from server, create channels and show notification. When my code is in the application, it works, but when I moved it to the android library, it doesn't work and in running mode application closed. what should I do? What steps do I need to correctly create SDK for my applications?
code to create channels
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
code to show notifications
btnShow.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View view) {
try {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification")
.setContentText("test")
.setChannelId(CHANNEL_ID)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
catch (Exception ex)
{
Log.i("Log Error",ex.getMessage());
}
}
});
These codes work on application itself but not in the library.