推送通知打开浏览器打开浏览器、通知

2023-09-06 11:22:06 作者:遍体鳞傷〃誰徻訫庝り

我试图打开浏览器的URL时,就推通知的用户点击,我在计算器搜索,我发现这个

 意图browserIntent =新的意图(Intent.ACTION_VIEW,Uri.parse(URL));
        startActivity(browserIntent);
 

但它不工作对我来说,当我把该通知的犯规出现,我调试它,它只能扔类文件编辑器没有任何错误或任何东西。

这是在code

 公共无效mostrarNotificacion(上下文的背景下,身体的字符串,字符串名称,字符串图标,URL字符串,字符串上级)
{

    字符串NS = Context.NOTIFICATION_SERVICE;
    NotificationManager notManager =(NotificationManager)context.getSystemService(NS);




    INT ICONO = R.drawable.mydrawable;
    CharSequence的textoEstado =优于;
    长霍拉= System.currentTimeMillis的();

    注意notif =新的通知(ICONO,textoEstado,霍拉);


    上下文contexto = context.getApplicationContext();
    CharSequence的TITULO =称号;
    CharSequence的descripcion =身体;
    PendingIntent contIntent;
    如果(url.equalsIgnoreCase(空))
    {
        意图notIntent =新的意图(contexto,MainActivity.class);
        contIntent = PendingIntent.getActivity(
                contexto,0,notIntent,0);
    }
    其他
    {
        //意图I =新的意图(Intent.ACTION_VIEW);
         //i.setData(Uri.parse(url));
        // contIntent = PendingIntent.getActivity(contexto,0,I,0);
        意图browserIntent =新的意图(Intent.ACTION_VIEW,Uri.parse(URL));
        startActivity(browserIntent);



    }


   // notif.setLatestEventInfo(contexto,TITULO,descripcion,contIntent);



    // AutoCancel:
    notif.flags | = Notification.FLAG_AUTO_CANCEL;


    //发送notif
    notManager.notify(1,notif);
}
 
手机qq浏览器在通知栏推送的新闻在哪能看,就是各种新闻,消息

解决方案

您需要做的是设置一个悬而未决的意图 - 其中,当用户点击该通知将被调用。 (以上你刚刚开始的活动...)

下面是一个示例code:

 私人无效createNotification(文本字符串,字符串link){

    NotificationCompat.Builder notificationBuilder =
        新NotificationCompat.Builder(本)
    .setAutoCancel(真)
    .setSmallIcon(R.drawable.app_icon)
    .setContentTitle(文本);

    NotificationManager mNotificationManager =
        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    //未决的目的是使用深层链接重定向
    意图resultIntent =新的意图(Intent.ACTION_VIEW);
    resultIntent.setData(Uri.parse(链接));

    PendingIntent未决= PendingIntent.getActivity(此,0,resultIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationBuilder.setContentIntent(待定);

    //使用相同的标签和Id导致新的通知,以取代现有的
    mNotificationManager.notify(将String.valueOf(System.currentTimeMillis的()),PUSH,notificationBuilder.build());
}
 

I am trying to open the browser with a url when the user click on the push notification, i search in stackoverflow and i find this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);

but it doesnt work for me, when i put that the notification doesnt appear, i debugged it and it only throw the class file editor no error or anything.

this is the code

   public void mostrarNotificacion(Context context, String body,String title, String icon, String url,String superior)
{

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notManager = (NotificationManager) context.getSystemService(ns);




    int icono = R.drawable.mydrawable;
    CharSequence textoEstado = superior;
    long hora = System.currentTimeMillis();

    Notification notif = new Notification(icono, textoEstado, hora);


    Context contexto = context.getApplicationContext();
    CharSequence titulo = title;
    CharSequence descripcion = body;
    PendingIntent contIntent;
    if(url.equalsIgnoreCase("NULL"))
    {
        Intent notIntent = new Intent(contexto,MainActivity.class);
        contIntent = PendingIntent.getActivity(
                contexto, 0, notIntent, 0);
    }
    else
    {            
        // Intent i = new Intent(Intent.ACTION_VIEW);
         //i.setData(Uri.parse(url));
        // contIntent = PendingIntent.getActivity(contexto, 0, i, 0);   
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);



    }


   // notif.setLatestEventInfo(contexto, titulo, descripcion, contIntent);



    //AutoCancel: 
    notif.flags |= Notification.FLAG_AUTO_CANCEL;


    //send notif
    notManager.notify(1, notif);
}

解决方案

What you need to do is set a pending intent - which will be invoked when the user clicks the notification. (Above you just started an activity...)

Here's a sample code :

private void createNotification(String text, String link){

    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.app_icon)
    .setContentTitle(text);

    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // pending intent is redirection using the deep-link
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(Uri.parse(link));

    PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationBuilder.setContentIntent(pending);

    // using the same tag and Id causes the new notification to replace an existing one
    mNotificationManager.notify(String.valueOf(System.currentTimeMillis()), PUSH, notificationBuilder.build());
}