Reputation: 87
I made an small app the shows five big tech giants called MAFAG (Microsoft, Apple, Facebook, Amazon, Google). I need no to create a really simple notification that looks like this:
The notification shows:
My app works fine and all. I am also able retrieve the values of (app name) and (app url) and place them in two variables. But my problem is that my notification does not work and the app closes when I click the notify button.
So here is my code for my MainActivity (keep in mind, I do not wish to open a new activity, simply to open a uri):
package uqac.dim.mafag;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public final static int code_retour=11;
private String url;
private String name;
private static final int ID_NOTIFICATION = 1234;
private static final int ID_CHANNEL = 1;
private NotificationManager nm;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
TextView nameCU = findViewById(R.id.verticalURL);
TextView nameC = findViewById(R.id.verticalCompany);
super.onActivityResult(requestCode, resultCode, data);
if (code_retour == requestCode && RESULT_OK == resultCode) {
name = data.getStringExtra(startActivityForResult.back);
url = data.getStringExtra(startActivityForResult.backU);
nameC.setText(name);
nameCU.setText(url);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
public void open(View v){
Uri webpage = Uri.parse(url);
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(webIntent);
}
public void choice(View v){
Intent intent = new Intent(MainActivity.this, startActivityForResult.class);
startActivityForResult(intent,code_retour);
}
public void creerNotification(View view) {
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, webIntent, 0);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Action action1 = new Notification.Action.Builder(
Icon.createWithResource(this,R.drawable.web_icon),
"MAFAG",
contentIntent).build();
Notification n = new Notification.Builder(this)
.setContentTitle("Ouvrir le MAFAG")
.setContentText(name)
.setSmallIcon(R.drawable.web_icon)
.setContentIntent(contentIntent)
.setSound(soundUri)
.setStyle(new Notification.BigTextStyle().bigText("IMPORTANT"))
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.addAction(action1)
.build();
try{
nm.notify(ID_NOTIFICATION, n);
}
catch(Exception e){
Log.i("DICJ", e.getMessage(), e);
}
}
}
Now, I know that the problem is within the code for my notification because eveything else works. I'm also pretty sure I don't properly understand how notifications work (even after watching and reading a bunch of docs on it).
I would really appreciate some help here, it's my first time using notifications and I'm screwed if I can't get it to work...
Thanks in advance!
Upvotes: 0
Views: 529
Reputation: 121
It looks like you never create a channel for this notification, you have a channel id but seems you never use it.
Try calling this function before you build your notification
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
// Register the channel
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
And when you create the notification builder, be sure to specify the channel id:
Notification n = new NotificationCompat.Builder(this, CHANNEL_ID)
...
.build();
Note that channels are only required after Android 8.0
Upvotes: 1