推送通知服务,为Android 4.4系统通知、系统、Android

2023-09-09 21:00:07 作者:冷丶月心

我已经做了推送通知及其工作正确的,但如果到API层4.0工作。但通知点击不在的情况下API 4.4打开活动....我不能够理解的答案,我有搜索奇巧并通知它使用Notification.Builder的API,它提供了相同的结果。

 私人无效generateNotification(上下文的背景下,消息字符串,字符串ID){
    INT图标= R.drawable.app_icon;
    时长= System.currentTimeMillis的();
    NotificationManager notificationManager =(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);

    通知通知=新的通知(图标,邮件,时);
    字符串标题= context.getString(R.string.app_name);

    JSONObject的jobj =新的JSONObject();

    尝试 {
        jobj.put(身份证,身份证);

    }赶上(JSONException E){
        e.printStackTrace();
    }
    的System.out.println(JSON对象+ jobj.toString());
    意图notificationIntent = NULL;

    notificationIntent =新的意图(背景下,JamInfo.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra(隆基,隆基);
    notificationIntent.putExtra(LATI,LATI);


    PendingIntent意图= PendingIntent.getActivity(上下文,0,
            notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);


    notification.setLatestEventInfo(背景下,标题,邮件,意图);


    notification.flags | = Notification.FLAG_AUTO_CANCEL;
    notification.defaults | = Notification.DEFAULT_VIBRATE;
    notification.defaults | = Notification.DEFAULT_LIGHTS;
     }

    notificationManager.notify((INT)System.currentTimeMillis的()
            通知);

}
 

解决方案

 这是工作为我的应用程序?试试这个...

私人无效showNotification(上下文的背景下){
        // TODO AK-生成方法存根
        字符串的appName = context.getString(R.string.app_name);
        NotificationCompat.Builder mBuilder =
                新NotificationCompat.Builder(上下文)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle(的appName)
                        .setContentText(的appName);

        开放的声音= Uri.parse(android.resource://+ context.getPackageName()+/生/+ audioToneName);
        mBuilder.setSound(音);
        mBuilder.setAutoCancel(真正的);
        mBuilder.setVibrate(Utility.vibrationPattern);
        //创建一个明确的意图在你的应用程序的活动
        意图resultIntent =新的意图(背景下,RootActivity.class);
        //堆栈生成器对象将包含人工回堆栈
        //的
        //开始活动。
        //这样可以确保从活动向后导航引领出
        //你的应用程序至主屏​​幕。
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(上下文);
        //添加背部栈的意图(而不是意图本身)
        stackBuilder.addParentStack(RootActivity.class);
        //添加的活动开始到堆栈的顶部的意图
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                        );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        // MID可以让你以后更新通知。
        mNotificationManager.notify(321,mBuilder.build());
    }
 

Android 服务器推送技术

I have done work on push notifications and its working correct but in case till api level 4.0. But notification click does not open the activity in case of api 4.4....I am not able to understand the answer, I have search on KitKat and for notifications it has use Notification.Builder Api,with which it gives the same result.

private void generateNotification(Context context, String message, String id) {
    int icon = R.drawable.app_icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);

    JSONObject jobj = new JSONObject();

    try {
        jobj.put("id", id);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    System.out.println("json object" + jobj.toString());
    Intent notificationIntent = null;

    notificationIntent = new Intent(context, JamInfo.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("longi", longi);
    notificationIntent.putExtra("lati", lati);


    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    notification.setLatestEventInfo(context, title, message, intent);


    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
     }

    notificationManager.notify((int) System.currentTimeMillis(),
            notification);

}

解决方案

This is working for my apps... Try this...

private void showNotification(Context context) {
        // TODO AK-generated method stub
        String appName = context.getString(R.string.app_name);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle(appName)
                        .setContentText(appName);

        Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + audioToneName);
        mBuilder.setSound(sound);
        mBuilder.setAutoCancel(true);
        mBuilder.setVibrate(Utility.vibrationPattern);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, RootActivity.class);
        // The stack builder object will contain an artificial back stack for
        // the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(RootActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                        );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(321, mBuilder.build());
    }